How do I create child windows with Python tkinter?

后端 未结 1 1481
耶瑟儿~
耶瑟儿~ 2020-11-29 06:40

I am using Python 3.3 and tkinter to make a GUI interface for a pedestrian fleeing simulation.

I\'ve written two simulation programs, and they worked well. However,I

相关标签:
1条回答
  • 2020-11-29 07:32

    You create child windows by creating instances of Toplevel. See http://effbot.org/tkinterbook/toplevel.htm for more information.

    Here's an example that lets you create new windows by clicking on a button:

    import Tkinter as tk
    
    class MainWindow(tk.Frame):
        counter = 0
        def __init__(self, *args, **kwargs):
            tk.Frame.__init__(self, *args, **kwargs)
            self.button = tk.Button(self, text="Create new window", 
                                    command=self.create_window)
            self.button.pack(side="top")
    
        def create_window(self):
            self.counter += 1
            t = tk.Toplevel(self)
            t.wm_title("Window #%s" % self.counter)
            l = tk.Label(t, text="This is window #%s" % self.counter)
            l.pack(side="top", fill="both", expand=True, padx=100, pady=100)
    
    if __name__ == "__main__":
        root = tk.Tk()
        main = MainWindow(root)
        main.pack(side="top", fill="both", expand=True)
        root.mainloop()
    
    0 讨论(0)
提交回复
热议问题