Python 3 and tkinter opening new window by clicking the button

后端 未结 1 1131
暗喜
暗喜 2021-01-18 14:19

How do I open a new window when the user clicks a button in Tkinter and Python 3?

相关标签:
1条回答
  • 2021-01-18 14:42

    You can open a new window by creating a new instance of the Tkinter class Toplevel.

    For example:

    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()
    
    0 讨论(0)
提交回复
热议问题