Bottom Page

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
 Using a class to create instances of Tkinter Toplevel() windows
#1
I'm a newbie at python and coding in general. I am messing around with Tkinter, trying to understand it. Before I go any further can someone tell me if it's ok practice to create a class to initialise Toplevel() windows?

This is my code, please pull it to pieces:

from tkinter import *

class TopLevelWindow():
    def __init__(self, master, width, height):
        self.master = master
        self.master.geometry("%sx%s+100+100" % (width, height))
        self.master.title("Toplevel Window")


root = Tk()
top1 = Toplevel()
top2 = Toplevel()
TopLevelWindow(top1, "300", "300")
TopLevelWindow(top2, "500", "250")

root.mainloop()
This code works but I really don't know what is happening, I just throw a few keywords together until it worked.
Can someone explain what is happening here where I pas in master and then define self.master as master. Is it even ALLOWED to be a master?

def __init__(self, master, width, height):
    self.master = master
Quote
#2
self is an instance of itself, in your case TopLevelWindow
when you call __init__, you are passing top1 which is an instance of TopLevel, width of 300, and height of 300.

If you passed root instead of top1, there would only be one window.
you can also 'hide' the root window with root.withdraw()

Toplevel creates a new 'root like' window in addition to root

Once top1 is passed to TopLevelWindow, it becomes self.master or TopLevelWindow.master, but it's still the same instance of top1

Then you create yet another instance of TopLevelWindow passing top2 as master

you could of also instantiated TopLevelWindow this way, which may explain a bit more:
TopLevelWindow(master=top1, width="300", height="300")
I try to do this by default, but when cranking out code, often miss doing it. It explains what's what with a single glance.
Quote
#3
(Mar-27-2018, 11:12 AM)Larz60+ Wrote: self is an instance of itself, in your case TopLevelWindow
when you call __init__, you are passing top1 which is an instance of TopLevel, width of 300, and height of 300.

If you passed root instead of top1, there would only be one window.
you can also 'hide' the root window with root.withdraw()

Toplevel creates a new 'root like' window in addition to root

Once top1 is passed to TopLevelWindow, it becomes self.master or TopLevelWindow.master, but it's still the same instance of top1

Then you create yet another instance of TopLevelWindow passing top2 as master

you could of also instantiated TopLevelWindow this way, which may explain a bit more:
TopLevelWindow(master=top1, width="300", height="300")
I try to do this by default, but when cranking out code, often miss doing it. It explains what's what with a single glance.

Hi Larz, thank you very much for the explanation. I think the main thing that's important here is that this seems a legitimate way of creating multi toplevel windows without breaking anything else in Python haha

I can now move forward with my project.

Big Grin
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 97 Jan-23-2020, 09:00 PM
Last Post: HBH
  Tkinter scaling windows conten to or with its size not working Detzi 5 155 Jan-12-2020, 12:42 PM
Last Post: Detzi
  How to close one of the windows in Tkinter scratchmyhead 3 266 Dec-21-2019, 06:48 PM
Last Post: pashaliski
  Unable fetch fucntion data in class in tkinter jenkins43 2 236 Nov-30-2019, 09:47 PM
Last Post: jenkins43
  Tkinter Class pythonenthusiast2 1 186 Nov-24-2019, 03:51 AM
Last Post: Larz60+
  [Tkinter] how can disable menu [About] when Toplevel is active balenaucigasa 0 230 Oct-25-2019, 09:49 PM
Last Post: balenaucigasa
  Using tkinter on Windows to launch python 3 scripts Mocap 1 260 Jul-17-2019, 05:16 AM
Last Post: Yoriz
  [Tkinter] Using tkinter and mutiprocess ok on windows, locks up on ubuntu? ice 3 471 May-29-2019, 08:44 AM
Last Post: ice
  Int Variables in different Tkinter windows only returning 0 harry76 3 612 May-26-2019, 10:24 AM
Last Post: Yoriz
  [Tkinter] Toplevel window and global widgets? KevinBrown 3 892 Apr-25-2019, 06:02 PM
Last Post: Yoriz

Forum Jump:


Users browsing this thread: 1 Guest(s)