tkinter:使用按钮提示打开新窗口 [英] tkinter: Open a new window with a button prompt
问题描述
如何通过用户在tkinter GUI中按下按钮来打开新窗口?我只需要一个非常简单的解决方案,并且如果代码也能得到解释,那就太好了。
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.
推荐答案
这是最短的您的问题的可能解决方案。该解决方案适用于python2.x。对于python 3.x,将导入更改为 tkinter而不是 Tkinter:
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":
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.
这篇关于tkinter:使用按钮提示打开新窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!