Python Forum

Full Version: Why is this opening 2 windows/frames?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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.
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()