框架未显示 | python tkinter
我想在我的窗口里显示一个框架,但不知道为什么窗口是空白的。这是我第一次用tkinter来使用类,我是不是需要在对象里调用它?
from customtkinter import *
class app(CTk):
def __init__(self, title, size):
# main setup
super().__init__()
self.title(title)
self.geometry(f'{size[0]}x{size[1]}')
self.minsize(size[0],size[1])
# widget
self.menu = CTkFrame(self)
# run
self.mainloop()
class Menu(CTkFrame):
def __init__(self, parent):
super().__init__(parent)
self.place(x=0,y=0, relwidth = 0.3, relheight = 1)
self.create_widgets()
def create_widgets(self):
heading_var = StringVar(value="Ethan")
def heading_combo(choice):
print(choice)
heading = CTkComboBox(self, value=["Ethan","Brock", "Liam"], command=heading_combo,variable=heading_var)
self.columnconfigure((0,1,2),weight=1, uniform='a')
self.rowconfigure((0,1,2,3,4),weight=1, uniform='a')
heading.pack()
app('Scoring Software', (600,600))
1 个回答
2
你看到空白窗口是因为你没有在self.menu这个小部件上调用pack()
、grid()
或者place()
方法。
要解决这个问题,你可以在第13行加上self.menu.pack()
。完整的代码如下:
from customtkinter import *
class app(CTk):
def __init__(self, title, size):
# main setup
super().__init__()
self.title(title)
self.geometry(f'{size[0]}x{size[1]}')
self.minsize(size[0], size[1])
# widget
self.menu = CTkFrame(self)
self.menu.pack()
# run
self.mainloop()
class Menu(CTkFrame):
def __init__(self, parent):
super().__init__(parent)
self.place(x=0, y=0, relwidth=0.3, relheight=1)
self.create_widgets()
def create_widgets(self):
heading_var = StringVar(value="Ethan")
def heading_combo(choice):
print(choice)
heading = CTkComboBox(self, values=["Ethan", "Brock", "Liam"], command=heading_combo, variable=heading_var)
self.columnconfigure((0, 1, 2), weight=1, uniform='a')
self.rowconfigure((0, 1, 2, 3, 4), weight=1, uniform='a')
heading.pack()
app('Scoring Software', (600, 600))
不过你的代码看起来有点奇怪,你创建了一个Menu类,但并没有使用它。
如果你以为这样class Menu(CTkFrame):
会完全覆盖CTkFrame类,那你就错了,它只是创建了一个带有修改方法的新类。
要使用你创建的Menu类,你应该把第12行替换成这样:
self.menu = self.Menu(self)
希望我能帮到你,祝你有个愉快的一天