Python Forum
Why is this opening 2 windows/frames?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why is this opening 2 windows/frames?
#1
from tkinter import *

class App(Tk):

    def __init__(self, root):

        Tk.__init__(self)

        self.geometry("1280x720")

        MainFrame = Frame(self, width=30, height=40, bg='blue')
        SecFrame = Frame(self, width=30, height=40, bg='red')
        MainFrame.grid(row=0, column=2)
        SecFrame.grid(row=1, column=2)

self = Tk()
App = App(self)
self.mainloop()
Very basic structure, but with something so basic, I cannot figure out why it continues to open 2 windows.

Solved it.
self = Tk()
App = App(self)
self.mainloop()
Needed to be changed to:
App = App()
App.mainloop()
to simplify further:
App().mainloop()
Reply
#2
I think it's because you pass Tk as an object to your class, and then (again I think) the init creates a new window.
Reply
#3
And it confuses Tkinter when there is more than one Tk instance. Note also that you are using the "self" and "App" names for two different variables each. https://micropyramid.com/blog/understand...hon-class/

from tkinter import *
 
class App():
 
    def __init__(self, root):
 
        root.geometry("1280x720")
 
        MainFrame = Frame(root, width=30, height=40, bg='blue')
        SecFrame = Frame(root, width=30, height=40, bg='red')
        MainFrame.grid(row=0, column=2)
        SecFrame.grid(row=1, column=2)
 
root = Tk()
Ap = App(root)
root.mainloop() 
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020