【问题标题】:Python 3 and tkinter opening new window by clicking the buttonPython 3 和 tkinter 通过单击按钮打开新窗口
【发布时间】:2013-02-07 03:17:19
【问题描述】:

当用户在 Tkinter 和 Python 3 中单击按钮时如何打开一个新窗口?

【问题讨论】:

    标签: python button tkinter window


    【解决方案1】:

    您可以通过创建 Tkinter 类 Toplevel 的新实例来打开一个新窗口。

    例如:

    import Tkinter as tk
    
    class View(tk.Frame):
        count = 0
        def __init__(self, *args, **kwargs):
            tk.Frame.__init__(self, *args, **kwargs)
            b = tk.Button(self, text="Open new window", command=self.new_window)
            b.pack(side="top")
    
        def new_window(self):
            self.count += 1
            id = "New window #%s" % self.count
            window = tk.Toplevel(self)
            label = tk.Label(window, text=id)
            label.pack(side="top", fill="both", padx=10, pady=10)
    
    if __name__ == "__main__":
        root = tk.Tk()
        view = View(root)
        view.pack(side="top", fill="both", expand=True)
        root.mainloop()
    

    【讨论】:

    • 好的,tnx 很多 :) 你能解释我几行代码吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 2017-06-14
    • 2015-09-09
    • 2018-10-24
    相关资源
    最近更新 更多