【问题标题】:Return to Menu Button TKinter返回菜单按钮 TKinter
【发布时间】: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


【解决方案1】:

好的,有几个建议:

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

【讨论】:

  • 我认为您对gridpack 的建议是错误的。 grid 绝对不会比 pack“更强大”。如果您在网格中排列小部件,它只会更强大。 pack 在从上到下或从一侧到另一侧安排事物时更胜一筹。您应该同时使用它们,利用它们各自的优势。事实上,您的代码是网格弱点之一的一个示例——您忽略了为任何行或列设置权重,这会给 UI 带来糟糕的调整大小行为。这是人们使用grid 时常犯的一个错误。
【解决方案2】:

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

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)

我不会这样写我的代码,但它显示了一般概念:使每个屏幕成为一个带有小部件的框架,然后一次隐藏/显示一个框架。这比拥有数百个您必须记住隐藏或显示的小部件更具可扩展性。

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

Switch between two frames in tkinter

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-15
    • 2016-12-07
    • 2014-03-16
    • 2013-03-20
    • 2018-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多