我尝试创建一个退出按钮来关闭 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 Points\n 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
答案 0 :(得分:1)
尝试这样做
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 Points\n 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()