Tkinter 小部件全部打包在同一个框架中
Tkinter widgets all pack in the same Frame
我正在尝试使用多个 windows 制作一个相当简单的 GUI。我现在将我的 windows 构建为 类,只是每个标签都有一个标签。我似乎无法弄清楚为什么当我 运行 我的程序时它包含了 "StartPage" 上的所有标签,而另一个 windows 的 none 上有任何内容。可能是我的 类 配置不正确?
import tkinter as tk
class application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side = 'top', fill = 'both', expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (StartPage, WeeklyBudget, LongtermSavings, Investments):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.ShowFrame(StartPage)
def ShowFrame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
start_label = tk.Label(self, text = 'Welcome to Finance Track!')
start_label.pack()
week_btn = tk.Button(self, text = 'Weekly Budgeting', command =lambda: controller.ShowFrame(WeeklyBudget))
savings_btn = tk.Button(self, text = 'Longterm Savings', command = lambda: controller.ShowFrame(LongtermSavings))
invest_btn = tk.Button(self, text = 'Investments', command = lambda: controller.ShowFrame(Investments))
week_btn.pack(pady = 10, padx = 10)
savings_btn.pack(pady = 10, padx = 10)
invest_btn.pack(pady = 10, padx = 10)
class WeeklyBudget(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Weekly Budget')
label.pack()
add_btn = tk.Button(text = 'add new week')
add_btn.pack()
class LongtermSavings(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Longterm Savings')
label.pack()
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Investments')
label.pack()
app = application()
app.mainloop()
我之前描述的当前结果只是一个 window,其中包含所有标签和所有按钮。
正如 jasonharper 提到的,您没有定义许多小部件的父级(又名主控)。
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Investments')
label.pack()
使用这个 Investments
class 例如,默认情况下,您的标签将被赋予 window 作为它的父级,要将它的父级设置为新创建的框架,只需执行以下:
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = 'Welcome to your Investments')
label.pack()
我正在尝试使用多个 windows 制作一个相当简单的 GUI。我现在将我的 windows 构建为 类,只是每个标签都有一个标签。我似乎无法弄清楚为什么当我 运行 我的程序时它包含了 "StartPage" 上的所有标签,而另一个 windows 的 none 上有任何内容。可能是我的 类 配置不正确?
import tkinter as tk
class application(tk.Tk):
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
container = tk.Frame(self)
container.pack(side = 'top', fill = 'both', expand = True)
container.grid_rowconfigure(0, weight = 1)
container.grid_columnconfigure(0, weight = 1)
self.frames = {}
for F in (StartPage, WeeklyBudget, LongtermSavings, Investments):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.ShowFrame(StartPage)
def ShowFrame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class StartPage(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
start_label = tk.Label(self, text = 'Welcome to Finance Track!')
start_label.pack()
week_btn = tk.Button(self, text = 'Weekly Budgeting', command =lambda: controller.ShowFrame(WeeklyBudget))
savings_btn = tk.Button(self, text = 'Longterm Savings', command = lambda: controller.ShowFrame(LongtermSavings))
invest_btn = tk.Button(self, text = 'Investments', command = lambda: controller.ShowFrame(Investments))
week_btn.pack(pady = 10, padx = 10)
savings_btn.pack(pady = 10, padx = 10)
invest_btn.pack(pady = 10, padx = 10)
class WeeklyBudget(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Weekly Budget')
label.pack()
add_btn = tk.Button(text = 'add new week')
add_btn.pack()
class LongtermSavings(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Longterm Savings')
label.pack()
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Investments')
label.pack()
app = application()
app.mainloop()
我之前描述的当前结果只是一个 window,其中包含所有标签和所有按钮。
正如 jasonharper 提到的,您没有定义许多小部件的父级(又名主控)。
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(text = 'Welcome to your Investments')
label.pack()
使用这个 Investments
class 例如,默认情况下,您的标签将被赋予 window 作为它的父级,要将它的父级设置为新创建的框架,只需执行以下:
class Investments(tk.Frame):
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
label = tk.Label(self, text = 'Welcome to your Investments')
label.pack()