0

When the below code is run there's an additional, empty window appears. Why does that happen, and how can it be fixed?

import tkinter as tk

class Deneme(tk.Tk):
    def __init__(self):
       super().__init__()
       self.smartGui()

def smartGui(self):
    tk.Label(self, text="Name").grid(row=0,column=0)
    tk.Entry(self).grid(row=0,column=1, columnspan=2)
    tk.Label(self, text="Surname").grid(row=1,column=0)
    tk.Entry(self).grid(row=1,column=1, columnspan=2)
    tk.Label(self, text="City").grid(row=0,column=4)
    tk.Entry(self).grid(row=0,column=5, columnspan=2)
    tk.Label(self, text="Explain").grid(row=3, column=0)

def main():
    root = tk.Tk()
    app = Deneme()
    root.mainloop()

if __name__ == "__main__":
   main()
Nae
  • 14,209
  • 7
  • 52
  • 79
aoyilmaz
  • 139
  • 1
  • 10

1 Answers1

2

Simplest way would be to replace:

def main():
    root = tk.Tk()
    app = Deneme()
    root.mainloop()

with:

def main():
    app = Deneme()
    app.mainloop()

There are two windows displayed, as there are two instances of Tk, root and app. app is an instance of Tk as well, as Deneme inherits from Tk.

Note: It's not suggested to have multiple instances of Tk. If you'd want multiple windows, later on, you should use Toplevel instead of Tk.


Additionally, you could simply inherit Deneme from Frame instead of Tk but you'd want to configure it a bit more for it to allow you to assign its parent as root. Then create its instance like app = Deneme(root).

Nae
  • 14,209
  • 7
  • 52
  • 79