2

我是编程新手,找不到一个教程,教如何创建使用多个窗口的 GUI。例如,如果用户单击“查找”按钮,则会弹出一个包含搜索结果的窗口。我该如何做到这一点?这在 Tkinter 中可能吗?任何对来源的建议/参考将不胜感激。谢谢。

4

2 回答 2

2

要创建您的第一个窗口,您需要创建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()
于 2013-08-20T21:40:55.100 回答
1
import tkinter as tk
from tkinter import *

class Example(tk.Frame):
    def __init__(self, root):
        tk.Frame.__init__(self, root)
        # b1 = tk.Button(self, text="Add another window", command = self.newWindow)
        self.count = 0
        self.canvas1 = Canvas(self, width=500, height=500)
        b1 = tk.Button(self.canvas1, text="Add another window", command = self.create_new_window)
        # b1.pack(side="top", padx=40, pady=40)
        self.canvas1.pack(side="top")
        b1.pack(side="top", padx=250, pady=240)


    def create_new_window(self):
        self.window1 = tk.Toplevel(self)
        self.window1.geometry("+160+300")
        self.canvas1 = Canvas(self.window1, width=50, height=500)
        # label = tk.Label(self.window1, text="This is window #%s" % self.count)
        self.canvas1.pack(side="top", fill="both")

    def create_new_window2(self):
        self.window2 = tk.Toplevel(self)
        self.canvas2 = Canvas(self.window2, width=500, height=500)
        # label = tk.Label(self.window2, text="This is window #%s" % self.count)
        self.canvas2.pack(side="top", fill="both", expand=True, padx=40, pady=40)


if __name__ == "__main__":
    root = tk.Tk()
    root.geometry("+300+300")
    Example(root).pack(side="top", fill="both", expand=True)
    root.mainloop()
于 2020-09-23T23:14:38.090 回答