我是编程新手,很难找到教如何创建使用多个窗口的 GUI 的教程。例如,如果用户单击“查找”按钮,则会弹出一个包含搜索结果的窗口。我该如何实现这个目标?这在 Tkinter 中可能吗? 任何对来源的建议/引用将不胜感激。 谢谢。
最佳答案
要创建第一个窗口,您需要创建 Tk
类的实例。所有其他窗口都是 Toplevel
的实例。
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, root):
tk.Frame.__init__(self, root)
b1 = tk.Button(self, text="Add another window", command = self.newWindow)
b1.pack(side="top", padx=40, pady=40)
self.count = 0
def newWindow(self):
self.count += 1
window = tk.Toplevel(self)
label = tk.Label(window, text="This is window #%s" % self.count)
label.pack(side="top", fill="both", expand=True, padx=40, pady=40);
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(side="top", fill="both", expand=True)
root.mainloop()
关于user-interface - Tkinter 中的多个窗口?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18342764/