0
我正在學習如何使用Okin方法使用tkinter創建父窗口和子窗口,但是在創建Toplevel窗口時不完全理解傳遞父窗口的「窗口」屬性的原因。見下文......爲什麼我們在創建Toplevel窗口時通過根窗口的'窗口'屬性?
from tkinter import *
class Main():
def __init__(self, master):
self.master = master
self.master.title("Main Window")
self.button1 = Button(self.master, text="Click Me 1", command = self.Open1)
self.button1.grid(row=0, column=0, sticky=W)
self.button2 = Button(self.master, text="Click Me 2", command = self.Open2)
self.button2.grid(row=0, column=2, sticky=W)
self.button3 = Button(self.master, text="Close", command = self.Close)
self.button3.grid(row=1, column=0, sticky=W)
def Open1(self):
second_window = Toplevel(self.master)
window2 = Second(second_window)
def Open2(self):
third_window = Toplevel(self.master)
window3 = Third(third_window)
def Close(self):
self.master.destroy()
class Second():
def __init__(self, master):
self.second_window = second_window
self.master.title("Second Window")
class Third():
def __init__(self, master):
self.master = master
self.master.title("Third Window")
root_window = Tk()
root_window.geometry("200x85")
main_window = Main(root_window)
root_window.mainloop()
然而,這兩個頂級窗口的創建也可以不帶參數的書面...它似乎產生相同的結果:
def Open1(self):
second_window = Toplevel()
window2 = Second(second_window)
def Open2(self):
third_window = Toplevel()
window3 = Third(third_window)
那麼,爲什麼我們在實例化頂層窗口時通常會傳遞此根窗口的屬性?
謝謝布萊恩。那麼這是否意味着如果沒有傳遞父對象(即將其作爲second_window = Toplevel()),使second_window成爲另一個根窗口,因此不是main_window的子對象? – sw123456
@ samwickins32:沒有。只能有一個根窗口。 Toplevel窗口不是也不能是根窗口。如果你沒有給頂層窗口一個父窗口,它的父窗口就是根窗口。 –
太棒了。非常感謝您的參與。總而言之,在創建Toplevel窗口時傳遞屬性的原因是明確指定它是哪個窗口的子窗口? – sw123456