我正在 Python 上用 tkinter 开发游戏程序,目前一直在尝试制定“返回主菜单”按钮,但无济于事。需要我帮忙吗?
到目前为止,这是我的代码:
from tkinter import *
from tkinter import ttk
root = Tk()
def MainMenu():
GameButton = Button(root, text="New Game", command = NewGame)
GameButton.pack()
GameLabel = Label(root, text="Click to create a new 2-player game", fg="blue", font=("Helvetica",16))
GameLabel.pack()
HelpButton = Button(root, text="Help", command = Help)
HelpButton.pack()
HelpLabel = Label(root, text="Click if you would like instructions", fg="orange", font=("Helvetica",16))
HelpLabel.pack()
ExitButton = Button(root, text="Exit",command = exit)
ExitButton.pack()
ExitLabel = Label(root, text="Click to exit application", fg="red", font=("Helvetica",16))
ExitLabel.pack()
InstructionsLabelFunc.pack_forget()
ReturnMenuFunc.pack_forget()
def NewGame():
GameButton.pack_forget()
ExitButton.pack_forget()
def Help():
GameButton.pack_forget()
HelpButton.pack_forget()
ExitButton.pack_forget()
GameLabel.pack_forget()
HelpLabel.pack_forget()
ExitLabel.pack_forget()
InstructionsLabel = InstructionsLabelFunc
InstructionsLabel.pack()
ReturnMenu = ReturnMenuFunc
ReturnMenu.pack()
def Exit():
exit()
GameButton = Button(root, text="New Game", command = NewGame)
GameButton.pack()
GameLabel = Label(root, text="Click to create a new 2-player game", fg="blue", font=("Helvetica",16))
GameLabel.pack()
HelpButton = Button(root, text="Help", command = Help)
HelpButton.pack()
HelpLabel = Label(root, text="Click if you would like instructions", fg="orange", font=("Helvetica",16))
HelpLabel.pack()
ExitButton = Button(root, text="Exit",command = exit)
ExitButton.pack()
ExitLabel = Label(root, text="Click to exit application", fg="red", font=("Helvetica",16))
ExitLabel.pack()
InstructionsLabelFunc = Label(root, text="""
Taken from nrich.maths.org
This is a collection of games of skill for two players, both players have exactly the same information, chance plays no part, and each game must terminate. There is always a 'winning strategy' and all the moves can be analysed mathematically. The only advantage that either player can possibly have is to start or to play second. To work out how to win you need to start by analysing the 'end game', and the losing position to be avoided, and then work back to earlier moves. Can you find the winning strategies?
The rules are simple. Start with any number of counters in any number of piles. Two players take turns to remove any number of counters from a single pile. The winner is the player who takes the last counter.""", fg="black", font=("Calibri", 14))
ReturnMenuFunc = Button(root, text="Return to Main Menu", command = MainMenu)
InstructionsLabelFunc.pack_forget()
ReturnMenuFunc.pack_forget()
mainloop()
请您参考如下方法:
好的,几个建议:
1) 将您的 Tkinter 信息保存在一个类中。当您正在制作的内容变得更加复杂时,这将使您更容易跟踪后续工作。
2) 使用grid()代替pack(),更加灵活和强大。
3) 使用 root.quit() 后接 sys.exit(0) 结束程序,而不仅仅是 exit() ,它更 Pythonic 和可靠。
4) 您可能应该使用 root.geometry 或其他方法为您的窗口定义一个固定大小,并确保文本环绕。目前还没有。
以下是您的代码的工作副本,其中实现了建议 1-3。如果您觉得答案有帮助,请勾选接受。
from tkinter import *
from tkinter import ttk
import sys
class GameGUI(object):
def __init__(self, root):
self.root = root
self.GameButton = Button(root, text="New Game", command=self.NewGame)
self.GameLabel = Label(root, text="Click to create a new 2-player game", fg="blue", font=("Helvetica",16))
self.HelpButton = Button(root, text="Help", command=self.Help)
self.HelpLabel = Label(root, text="Click if you would like instructions", fg="orange", font=("Helvetica",16))
self.ExitButton = Button(root, text="Exit",command=self.Exit)
self.ExitLabel = Label(root, text="Click to exit application", fg="red", font=("Helvetica",16))
self.InstructionsLabel = Label(root, text="""
Taken from nrich.maths.org
This is a collection of games of skill for two players, both players have exactly the same information, chance plays no part,
and each game must terminate. There is always a 'winning strategy' and all the moves can be analysed mathematically. The only
advantage that either player can possibly have is to start or to play second. To work out how to win you need to start by
analysing the 'end game', and the losing position to be avoided, and then work back to earlier moves. Can you find the winning strategies?
The rules are simple. Start with any number of counters in any number of piles. Two players take turns to remove any number of
counters from a single pile. The winner is the player who takes the last counter.""", fg="black", font=("Calibri", 14))
self.ReturnMenu = Button(root, text="Return to Main Menu", command=self.MainMenu)
self.MainMenu()
def MainMenu(self):
self.RemoveAll()
self.GameButton.grid()
self.GameLabel.grid()
self.HelpButton.grid()
self.HelpLabel.grid()
self.ExitButton.grid()
self.ExitLabel.grid()
def NewGame(self):
self.GameButton.grid_remove()
self.GameLabel.grid_remove()
self.ExitButton.grid_remove()
self.ExitLabel.grid_remove()
def Help(self):
self.RemoveAll()
self.InstructionsLabel.grid()
self.ReturnMenu.grid()
def RemoveAll(self):
self.GameButton.grid_remove()
self.GameLabel.grid_remove()
self.HelpButton.grid_remove()
self.HelpLabel.grid_remove()
self.ExitButton.grid_remove()
self.ExitLabel.grid_remove()
self.InstructionsLabel.grid_remove()
self.ReturnMenu.grid_remove()
def Exit(self):
self.root.quit
sys.exit(0)
if __name__ == '__main__':
root = Tk()
GameGUI = GameGUI(root)
root.mainloop()
