类型错误: __init__() 缺少 1 个必需的位置参数:'parent'



我尝试创建一个退出按钮来关闭Tkinter应用程序。到目前为止,一切都按计划进行,但如果我按下"退出"键。按钮,我收到一个错误。我不知道,我该如何改善。我试图用函数的方式来解决这个问题,但它不工作,以及(毫无疑问,我的错误)。谁能建议如何最好地整合这个按钮?

我代码:

from tkinter import *
import tkinter.ttk as ttk

class CollegeApp(Tk):
def __init__(self):
Tk.__init__(self)
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand=True)
self.frames = {}
for F in (StartPage, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()

class StartPage(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.startMenu()
def startMenu(self):
heading = Label(self, text="College Tournament Pointsn Count Software",
font=('Arial', 25))
heading.grid(row=0, column=0, columnspan=2, padx=240, pady=40)
start_Btn = Button(self, text="Start", font="Arial 16", width=8,
command=lambda: self.controller.show_frame(PageTwo))
start_Btn.grid(row=1, column=0, padx=30)
exit_Btn = Button(self, text="EXIT", font="Arial 16", width=8,
command=exitButton)
exit_Btn.grid(row=1, column=1, padx=30)
def starting_Program():
pass
class exitButton(Button):
def __init__(self, parent):
Button.__init__(self, parent)
self[Button] = parent.destroy
self.pack(BOTTOM)

class PageTwo(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.make_widget()
def make_widget(self):
ttk.Label(self, text='This is page two').grid(padx=(20, 20), pady=(20, 20))
button1 = ttk.Button(self, text='Previous Page',
command=lambda: self.controller.show_frame(StartPage))
button1.grid()

if __name__ == '__main__':
app = CollegeApp()
app.geometry("800x500")
app.title('Points Counter')
app.mainloop()

错误:

/Users/aleks/PycharmProjects/PointCounter/venv/bin/python /Users/aleks/PycharmProjects/PointCounter/main.py
Exception in Tkinter callback
Traceback (most recent call last):
File "/Library/Frameworks/Python.framework/Versions/3.9/lib/python3.9/tkinter/__init__.py", line 1892, in __call__
return self.func(*args)
TypeError: __init__() missing 1 required positional argument: 'parent'
Process finished with exit code 0

试试

from tkinter import *
import tkinter.ttk as ttk

class CollegeApp(Tk):
def __init__(self):
Tk.__init__(self)
container = ttk.Frame(self)
container.pack(side="top", fill="both", expand=True)
self.frames = {}
for F in (StartPage, PageTwo):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame(StartPage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()

class StartPage(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.startMenu()
def startMenu(self):
heading = Label(self, text="College Tournament Pointsn Count Software",
font=('Arial', 25))
heading.grid(row=0, column=0, columnspan=2, padx=240, pady=40)
start_Btn = Button(self, text="Start", font="Arial 16", width=8,
command=lambda: self.controller.show_frame(PageTwo))
start_Btn.grid(row=1, column=0, padx=30)
exit_Btn = Button(self, text="EXIT", font="Arial 16", width=8,
command=self.controller.destroy)
exit_Btn.grid(row=1, column=1, padx=30)
def starting_Program():
pass

class PageTwo(ttk.Frame):
def __init__(self, parent, controller):
self.controller = controller
ttk.Frame.__init__(self, parent)
self.make_widget()
def make_widget(self):
ttk.Label(self, text='This is page two').grid(padx=(20, 20), pady=(20, 20))
button1 = ttk.Button(self, text='Previous Page',
command=lambda: self.controller.show_frame(StartPage))
button1.grid()

if __name__ == '__main__':
app = CollegeApp()
app.geometry("800x500")
app.title('Points Counter')
app.mainloop()

相关内容

  • 没有找到相关文章

最新更新