Python 返回菜单按钮TKinter

Python 返回菜单按钮TKinter,python,python-3.x,button,tkinter,menu,Python,Python 3.x,Button,Tkinter,Menu,我正在用Python上的tkinter为一个游戏开发一个程序,目前我一直在尝试设计一个“返回主菜单”按钮,但没有成功。能帮我点忙吗 以下是我目前的代码: from tkinter import * from tkinter import ttk root = Tk() def MainMenu(): GameButton = Button(root, text="New Game", command = NewGame) GameButton.pack() GameL

我正在用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) 把你的资料放在课堂上。这将使您更容易在以后的工作中跟踪您正在制作的内容变得更加复杂的情况

2) 使用
grid()
而不是
pack()
,它更灵活、更强大

3) 使用
root.quit()
后跟
sys.exit(0)
来结束程序,而不仅仅是
exit()

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()

这类问题的最佳解决方案是使每个“屏幕”成为一个框架,其中包含您想要在屏幕中显示的任何内容。例如:

def help_screen(parent):
    screen = Frame(parent, ...)
    label = Label(screen, ...)
    ...
    return screen

def other_screen(parent):
    screen = Frame(parent, ...)
    ...
    return screen
然后,您的主程序只需要隐藏或销毁屏幕本身,而不是试图隐藏屏幕上的所有小部件:

def show_screen(screen):
    global current_screen
    if current_screen is not None:
        current_screen.pack_forget()
    current_screen = screen
    screen.pack(...)
您的初始代码可能如下所示:

help = help_screen(root)
other = other_screen(root)
current_screen = None
show_screen(help_screen)
我不会这样写我的代码,但它展示了一般的概念:用小部件将每个屏幕设为一个框架,然后一次隐藏/显示一个框架。这比你必须记住隐藏或显示数百个小部件更具可伸缩性

有关管理多个屏幕的面向对象方法,请参见以下问题:


回答问题后,请不要像那样删除问题的文本。Stack Overflow是一个问题和答案的存储库,面向未来读者,而不仅仅是提问者。我认为你关于
grid
vs
pack
的建议是错误的<代码>网格
肯定不会比
更“强大”。只有在网格中排列小部件时,它才更强大<代码>包装在将物品从上到下或从一边到另一边排列时更具优势。你应该两者兼用,利用各自的优势。事实上,您的代码就是网格弱点之一的一个例子——您忽略了为任何行或列设置权重,这将导致UI错误的调整大小行为。这是人们在使用
网格时经常犯的错误。