为什么我不能使用TKinter正确切换到新帧?

时间:2014-12-09 06:57:57

标签: python tkinter containers

我在TKinter中使用容器类从菜单屏幕开始,并且可以在必要时切换到另外两个屏幕中的一个。它不是很正常,我不知道为什么。

以下是我的主菜单屏幕:

http://i62.tinypic.com/2nhoju0.jpg(暂不让我发布图片)

当我点击New Game时,它看起来像这样:

http://i57.tinypic.com/x3tglg.jpg(暂不让我发布图片)

显然,新游戏和继续按钮不应该在那里,并且应该只有一个退出按钮。顶部的标签应该像主菜单屏幕一样。

谁能告诉我为什么会这样?

以下是相关代码:

class Battleship():
    def __init__(self, root):
        self.root = root
        self.pages = {}

        container = Frame(root)
        container.pack(side="top", fill="both", expand=True)
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        for P in (MainMenu, ShipSelect, BattleScreen):
            frame = P(container, self)
            frame.grid(row=0, column=0, sticky="nsew")
            self.pages[P] = frame
        self.show_frame(MainMenu)

    def show_frame(self, cont):
        frame = self.pages[cont]
        frame.tkraise()

class MainMenu(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="Welcome to PyBattleship!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)


        button1 = Button(root, text='New Game', width=13, command=lambda :controller.show_frame(ShipSelect))
        button1.pack()
        button2 = Button(root, text='Continue Game', width=13)
        button2.pack()
        button3 = Button(root, text='Quit', width=13, command=lambda controller=controller:controller.root.destroy())
        button3.pack()

class ShipSelect(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        self.root = root
        label = Label(self, text="Please place your ships", font=LARGE_FONT)

        button1 = Button(self, text='Quit', width=13, command=lambda    controller=controller:controller.root.destroy())
        button1.pack()

root = Tk()
root.title('Py-Battleship')
sheet = Battleship(root)
root.mainloop()

2 个答案:

答案 0 :(得分:0)

我刚才意识到我还在使用' root'对于MainMenu中的按钮小部件。当我改变了根源'自我'并且在ShipSelect中为标签添加了填充,它似乎有效。

class MainMenu(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="Welcome to PyBattleship!", font=LARGE_FONT)
        label.pack(pady=10,padx=10)


        button1 = Button(self, text='New Game', width=13, command=lambda controller=controller:controller.show_frame(ShipSelect))
        button1.pack()
        button2 = Button(self, text='Continue Game', width=13)
        button2.pack()
        button3 = Button(self, text='Quit', width=13, command=lambda controller=controller:controller.root.destroy())
        button3.pack()



class ShipSelect(Frame):
    def __init__(self, parent, controller):
        Frame.__init__(self, parent)
        label = Label(self, text="Please place your ships", font=LARGE_FONT)
        label.pack(pady=10,padx=10)

        button1 = Button(self, text='Quit', width=13, command=lambda controller=controller:controller.root.destroy())
        button1.pack()

答案 1 :(得分:-1)

在tkinter中使用网格和包的混合从来不是一个好主意,这是您的代码可能具有不可预测结果的原因之一。其次,您尝试将按钮打包到根窗口,而不是您创建的其中一个框架。