Python 将参数传递给TkInter帧
我是一名教师,每周我给学生的参与分数打分。因为我教计算机,我想我应该创建一个程序来为我处理这个逻辑。我计划使用TkInter创建一个包含一天中4个时段的开始屏幕,根据时段的不同,它会拉高该类。但我尝试在所有4个句点中使用相同的类,因为代码完全相同 这是我的密码:Python 将参数传递给TkInter帧,python,tkinter,Python,Tkinter,我是一名教师,每周我给学生的参与分数打分。因为我教计算机,我想我应该创建一个程序来为我处理这个逻辑。我计划使用TkInter创建一个包含一天中4个时段的开始屏幕,根据时段的不同,它会拉高该类。但我尝试在所有4个句点中使用相同的类,因为代码完全相同 这是我的密码: class part(tk.Tk): #Creates a class for the GUI def __init__(self, *args, **kwargs): #Initialization
class part(tk.Tk):
#Creates a class for the GUI
def __init__(self, *args, **kwargs):
#Initialization function of partGUI
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.iconbitmap(self, default="") #default icon in an .ico file
tk.Tk.wm_title(self, "Lucey's Participation Program") #title
window = tk.Frame(self)
window.pack(side="top", fill="both", expand=True)
window.grid_rowconfigure(0, weight=1)
window.grid_columnconfigure(0, weight=1)
self.frames= {}
for F in (StartPage, ClassPart, SettingsPage):
frame = F(window, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, window):
#Allows Program to switch windows/pages/frames
frame = self.frames[window]
frame.tkraise()
class StartPage(tk.Frame):
# Home Screen for Program
def __init__(self, parent, controller):
tk.Frame.__init__(self, parent)
title = tk.Label(self, text="Participation Scores", font=LARGE_FONT)
title.pack(pady=10, padx=10)
btnPeriod1 = tk.Button(self, text="1st Period", fg="red",
command=lambda: controller.show_frame(ClassPart(controller, 1)))
btnPeriod1.pack()
class ClassPart(tk.Frame):
# Screen showing students, participation buttons & their scores/Hogwarts houses
def __init__(self, parent, controller, period):
tk.Frame.__init__(self, parent)
但这带来了一个错误:
Traceback (most recent call last):
File "/home/klucey/Documents/partvers2.py", line 307, in <module>
window = part()
File "/home/klucey/Documents/partvers2.py", line 40, in __init__
frame = F(window, self)
TypeError: __init__() missing 1 required positional argument: 'period'
回溯(最近一次呼叫最后一次):
文件“/home/klucey/Documents/partvers2.py”,第307行,在
窗口=部分()
文件“/home/klucey/Documents/partvers2.py”,第40行,in__init__
框架=F(窗,自)
TypeError:\uuuu init\uuuu()缺少1个必需的位置参数:“句点”
对初学者/中级的任何帮助都将不胜感激 您(和其他人似乎)用来处理多页Tkinter应用程序的样板代码根本不适合处理同一类的多页。您必须将多次出现的
ClassPart
放在页面列表中,并在构造时以某种方式安排给它们一个不同的period
参数-但这打破了.show_frame()
方法,因为您不再有唯一的标识符来选择要显示的页面
以下是我的建议,因为您有一组固定的页面(这对动态生成的页面不太适用):
- 去掉类中的
参数(以便其构造函数与其他页面兼容)period
- 为每个期间创建一个子类:
。。。等等请参阅基类中的class ClassPart1(ClassPart): period = 1 class ClassPart2(ClassPart): period = 2
,以访问此值self.period
- 将F in的初始页面创建循环更改为
。使用(起始页面、ClassPart1、ClassPart2、ClassPart3、ClassPart4、设置页面):
等作为传递到ClassPart1
的标识符。show_frame()
类零件
创建初始化
函数时,在末尾有一个名为period的参数,当您稍后创建类零件
对象时,您没有包含此参数,如果包含它,它应该可以工作