2016-06-25 162 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