Tkinter example code for multiple windows, why won't buttons load correctly?

前端 未结 5 1920
独厮守ぢ
独厮守ぢ 2020-12-02 07:44

I am writing a program which should:

  1. Open a window with the press of a button.
  2. Close the newly opened window with the press of another button.
5条回答
  •  误落风尘
    2020-12-02 08:28

    #!/usr/bin/env python
    import Tkinter as tk
    
    from Tkinter import *
    
    class windowclass():
    
            def __init__(self,master):
                    self.master = master
                    self.frame = tk.Frame(master)
                    self.lbl = Label(master , text = "Label")
                    self.lbl.pack()
                    self.btn = Button(master , text = "Button" , command = self.command )
                    self.btn.pack()
                    self.frame.pack()
    
            def command(self):
                    print 'Button is pressed!'
    
                    self.newWindow = tk.Toplevel(self.master)
                    self.app = windowclass1(self.newWindow)
    
    class windowclass1():
    
            def __init__(self , master):
                    self.master = master
                    self.frame = tk.Frame(master)
                    master.title("a")
                    self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25 , command = self.close_window)
                    self.quitButton.pack()
                    self.frame.pack()
    
    
            def close_window(self):
                    self.master.destroy()
    
    
    root = Tk()
    
    root.title("window")
    
    root.geometry("350x50")
    
    cls = windowclass(root)
    
    root.mainloop()
    

提交回复
热议问题