2016-06-25 121 views
2

我正在为Python中的tkinter开发一款游戏的程序,并且此刻一直试图制定一个“返回主菜单”按钮,但无济于事。请帮我一下吗?返回菜单按钮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

请不要_not_删除像你的问题的文本,它已经回答了。堆栈溢出是问题和答案的存储库,为未来的读者带来好处,而不仅仅是提出问题的人。 –

回答

2

好吧,几点建议:

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

我认为你对'grid'和'pack'的建议是错误的。 'grid'绝对不比'pack'更“强大”。如果您在网格中安排小部件,它将更加强大。当从上到下或左右排列物体时,'pack'优越。你应该使用他们两个,使用他们各自的优势。事实上,你的代码是一个网格弱点的例子 - 你忽略为任何行或列设置权重,这会给UI带来不好的尺寸变化行为。这是人们用'grid'制作的一个非常常见的错误。 –

0

这种类型的问题的最佳解决方案是使每个“屏幕”与任何你想在屏幕上的框架。例如:

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