简体   繁体   English

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

[英]Why can't I properly switch to a new frame using TKinter?

I am using a container class in TKinter to start with a Menu screen and be able to switch to one of two other screens when necessary. 我在TKinter中使用容器类从“菜单”屏幕开始,并且能够在必要时切换到其他两个屏幕之一。 It isn't quite working correctly and I'm not sure why. 它不能正常工作,我不确定为什么。

Here is what my main menu screen looks like: 我的主菜单屏幕如下所示:

http://i62.tinypic.com/2nhoju0.jpg (won't let me post images yet) http://i62.tinypic.com/2nhoju0.jpg (不会让我发布图像)

When I click New Game, it looks like this: 当我单击“新建游戏”时,它看起来像这样:

http://i57.tinypic.com/x3tglg.jpg (won't let me post images yet) http://i57.tinypic.com/x3tglg.jpg (还不允许我发布图像)

Clearly, the new game and continue buttons are not supposed to be there, and there should only be one quit button. 显然,不应有新游戏和继续按钮,而应该只有一个退出按钮。 There should also be a label at the top kinda like the main menu screen. 像主菜单屏幕一样,顶部也应该有一个标签。

Can anyone tell me why this is happening? 谁能告诉我为什么会这样吗?

Here is the relevant code: 以下是相关代码:

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

I just realized that I was still using 'root' for the button widgets in MainMenu. 我只是意识到我仍在MainMenu中的按钮小部件上使用“ root”。 When I changed the 'roots' to 'self' and added padding to the label in ShipSelect, it seems to work. 当我将“ roots”更改为“ self”并在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()

Using a mixture of grid and pack in tkinter is never a good idea, which is one of the reasons your code might be having unpredictable results. 在tkinter中使用网格和pack的混合物永远不是一个好主意,这是您的代码可能会产生不可预测的结果的原因之一。 Secondly, you are trying to pack the buttons to the root window, rather than one of the frames you have created. 其次,您尝试将按钮打包到根窗口,而不是已创建的框架之一。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM