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

There must be something very obvious about why this code is incorrect. I hope someone can point this out.

I am new to Python and even new to Tkinter. You can say?; )

The reason I use classes is because it is just a sample code for me to get the essence of things, then it will be inserted into a larger program.

I tried to keep it as simple as possible. I want to program:

  • Open the window by pressing the button.
  • Close the newly opened window by pressing another button.

    import tkinter as tk from tkinter import * from tkinter import ttk class Demo1( Frame ): def __init__( self ): tk.Frame.__init__(self) self.pack() self.master.title("Demo 1") self.button1 = Button( self, text = "Button 1", width = 25, command = self.new_window ) self.button1.grid( row = 0, column = 1, columnspan = 2, sticky = W+E+N+S ) def new_window(self): self.newWindow = Demo2() class Demo2(Frame): def __init__(self): new =tk.Frame.__init__(self) new = Toplevel(self) new.title("Demo 2") new.button = tk.Button( text = "Button 2", width = 25, command = self.close_window ) new.button.pack() def close_window(self): self.destroy() def main(): Demo1().mainloop() if __name__ == '__main__': main() 
+30
python class tkinter destroy
Apr 20 '13 at 0:46
source share
3 answers

I rewrote your code in a more organized, better practical way:

 import tkinter as tk class Demo1: def __init__(self, master): self.master = master self.frame = tk.Frame(self.master) self.button1 = tk.Button(self.frame, text = 'New Window', width = 25, command = self.new_window) self.button1.pack() self.frame.pack() def new_window(self): self.newWindow = tk.Toplevel(self.master) self.app = Demo2(self.newWindow) class Demo2: def __init__(self, master): self.master = master self.frame = tk.Frame(self.master) self.quitButton = tk.Button(self.frame, text = 'Quit', width = 25, command = self.close_windows) self.quitButton.pack() self.frame.pack() def close_windows(self): self.master.destroy() def main(): root = tk.Tk() app = Demo1(root) root.mainloop() if __name__ == '__main__': main() 
+44
Apr 20 '13 at 1:33
source share

You need to specify a wizard for the second button. Otherwise, it will be packed in the first window. This is necessary not only for Button , but also for other widgets and objects other than gui, such as StringVar .

Quick fix: add a new frame as the first argument to your Button in Demo2 .

Perhaps better: You currently have Demo2 inheriting from tk.Frame , but I think it makes sense if you change Demo2 to something like this,

 class Demo2(tk.Toplevel): def __init__(self): tk.Toplevel.__init__(self) self.title("Demo 2") self.button = tk.Button(self, text="Button 2", # specified self as master width=25, command=self.close_window) self.button.pack() def close_window(self): self.destroy() 

Just like a suggestion, you should only import tkinter once. Select one of the first two import statements.

+11
Apr 20 '13 at 1:28
source share
 #!/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() 
+4
Aug 04 '14 at
source share



All Articles