【发布时间】:2016-10-28 00:15:56
【问题描述】:
我正在为 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()
【问题讨论】:
-
请不要不要在您的问题得到回答后删除这样的文本。 Stack Overflow 是一个问题和答案的存储库,供未来读者使用,而不仅仅是提问者。
标签: python python-3.x button tkinter menu