我刚开始学习python,但遇到一个问题,我无法获得关闭程序的按钮。代码:
from tkinter import *
import ScoreboardController as SC
class guiController(Frame):
def open_scoreboard(self):
scoreBoard = SC.ScoreboardController("scores.txt")
for x in range(len(scoreBoard)):
print("Name: {} \nScore: {}". format(scoreBoard[x].name, scoreBoard[x].score))
def start_game(self):
pass
def hide_main_window(self):
self.score_button.pack_forget()
self.start_button.pack_forget()
def create_widgets(self):
self.frame = Frame(master=None, width=800, height=600)
self.frame.pack()
self.start_button = Button(self.frame)
self.start_button["text"] = "Játék indítása"
self.start_button["bg"] = "#5E99FF"
self.start_button["fg"] = "#ffffff"
self.start_button["command"] = self.start_game
self.start_button.pack()
self.start_button.place(x=300, y=455, bordermode=OUTSIDE, height=50, width=200)
self.score_button = Button(self.frame)
self.score_button["text"] = "Eredmények"
self.score_button["bg"] = "#5E99FF"
self.score_button["fg"] = "#ffffff"
self.score_button["command"] = self.open_scoreboard
self.score_button.pack()
self.score_button.place(x=300, y=400, bordermode=OUTSIDE, height=50, width=200)
self.quit_button = Button(self.frame)
self.quit_button["text"] = "Kilépés"
self.quit_button["bg"] = "#5E99FF"
self.quit_button["fg"] = "#FFFFFF"
self.quit_button["command"] = self.destroy()
self.quit_button.pack()
self.quit_button.place(x=300, y=510, bordermode=OUTSIDE, height=50, width=200)
def __init__(self, master=None):
Frame.__init__(self, master)
self.place()
self.create_widgets()
当我单击按钮时,它什么也没做(self.quit_button)。 其他按钮起作用。
提前谢谢
答案 0 :(得分:0)
我无法完全测试您的代码,但是这是需要销毁master时要执行的操作。您的代码有点难以测试,请以后不要包含ScoreboardController
之类无法测试的内容。我也将您的按钮改写为更简单的东西。
import tkinter as tk
class Example(tk.Frame):
def __init__(self, master):
tk.Frame.__init__(self, master)
self.create_widgets()
def start_game(self):
pass
def hide_main_window(self):
self.score_button.pack_forget()
self.start_button.pack_forget()
def create_widgets(self):
self.start_button = tk.Button(self, text="Játék indítása", bg="#5E99FF", fg="#ffffff")
self.start_button.pack()
self.score_button = tk.Button(self, text="Eredmények", bg="#5E99FF", fg="#ffffff", command=self.start_game)
self.score_button.pack()
self.quit_button = tk.Button(self, text="Kilépés", bg="#5E99FF", fg="#ffffff", command=self.master.destroy) # without ()
self.quit_button.pack()
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack() # root passing to master in the Frame class
root.mainloop()