Python tkinter:打开一个带有按钮提示的新窗口

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27639298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 02:03:04  来源:igfitidea点击:

tkinter: Open a new window with a button prompt

pythonbuttontkinterwindow

提问by Eddy Loring

How would I be able to open a new window by the user pressing a button in a tkinter GUI? I only need quite simple solutions, and if the code could be explained as well that would be great.

我如何能够通过用户按下 tkinter GUI 中的按钮来打开一个新窗口?我只需要非常简单的解决方案,如果代码也可以解释,那就太好了。

回答by Bryan Oakley

Here's the nearly shortest possible solution to your question. The solution works in python 2.x. For python 3.x change the import to "tkinter" rather than "Tkinter":

这是您问题的几乎最短的解决方案。该解决方案适用于 python 2.x。对于 python 3.x,将导入更改为“tkinter”而不是“Tkinter”:

import Tkinter as tk

def create_window():
    window = tk.Toplevel(root)

root = tk.Tk()
b = tk.Button(root, text="Create new window", command=create_window)
b.pack()

root.mainloop()

This is definitely not what I recommend as an example of good coding style, but it illustrates the basic concepts: a button with a command, and a function that creates a window.

这绝对不是我推荐的良好编码风格示例,但它说明了基本概念:带有命令的按钮和创建窗口的函数。