Skip to content Skip to sidebar Skip to footer

Tkinter Won't Open More Than Two Windows

I'm working on a program in python using Tkinter. The issue I currently have is that I have buttons that open another windows. My issue is that it can't open more than two windows.

Solution 1:

You forgot in second window

self.master = master

and you get error message when you try to open third window.

But you should rather assign Toplevel(master) to self. master and then use self.master instead of self.top

from tkinter import *


classFirst(object):

    def__init__(self, master):
        self.master = master # = Tk()
        self.btn = Button(self.master, text="First", command=self.second)
        self.btn.grid(row=0, column=0)

    defsecond(self):
        self.w = Second(self.master)
        self.master.wait_window(self.w.master)


classSecond(object):

    def__init__(self, master):
        self.master = Toplevel(master)
        self.btn = Button(self.master, text="Second", command=self.third)
        self.btn.grid(row=0, column=0)

    defthird(self):
        self.w = Third(self.master)
        self.master.wait_window(self.w.master)


classThird(object):

    def__init__(self, master):
        self.master = Toplevel(master)
        self.btn = Button(self.master, text="Third")
        self.btn.grid(row=0, column=0)


if __name__ == "__main__":
    root = Tk()
    first = First(root)
    root.mainloop()

And now all classes looks very similar - they have self.master = Tk() or self.master = Toplevel(master)


BTW: use CamelCase names for classes - First, Second, Third - it helps recognize classes in code.

Solution 2:

I suggest that all your classes inherit from Toplevel instead of object, this way, all your classes will have a master attribute (which is why you had an error, the second class does not have one):

from tkinter import *

classFirst(Toplevel):
    def__init__(self, master):
        Toplevel.__init__(self, master)
        self.admin_btn = Button(self, text = "First", command = self.second)
        self.admin_btn.grid(row=0, column=0)

    defsecond(self):
        self.w = Second(self)
        self.wait_window(self.w)

classSecond(Toplevel):
    def__init__(self, master):
        Toplevel.__init__(self, master)
        self.second = Button(self, text = "Second", command = self.third)
        self.second.grid(row=0, column=0)

    defthird(self):
        self.w = Third(self.master)
        self.wait_window(self.w)

classThird(Toplevel):
    def__init__(self, master):
        Toplevel.__init__(self, master)
        self.second = Button(self, text = "Third")
        self.second.grid(row=0, column=0)


if __name__ == "__main__":
    root = Tk()
    m = First(root)
    m.lift(root)
    root.wait_window(m)
    root.mainloop()

Solution 3:

Try using this code:

import tkinter as tk

classDemo1:
    def__init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.button1 = tk.Button(self.frame, text = 'First', width = 25, command = self.new_window)
        self.button1.pack()
        self.frame.pack()
    defnew_window(self):
        self.newWindow = tk.Toplevel(self.master)
        self.app = Demo2(self.newWindow)

classDemo2:
    def__init__(self, master):
        self.master = master
        self.frame = tk.Frame(self.master)
        self.quitButton = tk.Button(self.frame, text = 'Second', width = 25, command = self.close_windows)
        self.quitButton.pack()
        self.frame.pack()
    defclose_windows(self):
        self.master.destroy()

defmain(): 
    root = tk.Tk()
    app = Demo1(root)
    root.mainloop()

if __name__ == '__main__':
    main()

In this code I have added two windows but you can add a third one.

Post a Comment for "Tkinter Won't Open More Than Two Windows"