from tkinter import *
window = Tk()
sample = Label(text="some text")
sample.pack()
sample2 = Label(text="some other text")
sample2.pack()
sample.mainloop()
Whats the difference between sample.mainloop() and window.mainloop()?
Why should window be included in sample(window, text ="some text") as the program runs without it.
sample.mainloopandwindow.mainloopcall the same function internally so they are the same. They both go in awhile Trueloop while updating the GUI. They can only exit from the loop when.quitorwindow.destroyis called.This is the code from
tkinter/__init__.pyline 1281:Both
LabelandTkinherit fromMiscso both of them use that same method. From this:You can see that both of the
tkobjects are the same object.For this line:
sample = Label(text="some text"), it doesn't matter if you putwindowas the first arg. It only matters if you have multiple windows as tkinter wouldn't know which window you want.When you have 1 window, tkinter uses that window. This is the code from tkinter/init.py line 2251:
tkinter
Labelinherits fromWidgetwhich inherits fromBaseWidget.