在Tkinter中打包和拆包框架时遇到问题

时间:2015-04-07 22:44:31

标签: python python-3.x tkinter

我在Python中编写了一个相当简单的程序,但是它使用了Tkinter,我在使用pack()pack_forget()时遇到了一些问题。我在Python 3.4中。基本上,我目前有一个简单的程序,有三个按钮:播放,说明和设置。没问题。 Play按钮命令只在具有三个按钮的pack_forget()上执行Frame(main)

因此,我需要为每个播放按钮分别使用三个play()命令,因为每个命令在不同的帧上调用pack_forget()。有没有办法在当前打包的任何帧上执行pack_forget()所以我只能为所有三个播放按钮调用一个play()函数?

(以下是代码的样子,当然简化了):

def play_1():
    title.pack_forget()
    game.pack()

def play_2():
    instructions.pack_forget()
    game.pack()

def play_3():
    settings.pack_forget()
    game.pack()

from tkinter import *
main = Tk()

title = Frame(main).pack()
instructions = Frame(main)
settings = Frame(main)

play_button1 = Button(title, text="Play", command=play_1)
play_button2 = Button(instructions, text="Play", command=play_2)
play_button3 = Button(settings, text="Play", command=play_3)

我只是在寻找一种解决方案,允许我将play_1()play_2()play_3()合并到一个函数中。

1 个答案:

答案 0 :(得分:2)

使用变量currentFrame并在编写时例如

instructions.pack()

添加

currentFrame = instructions

然后你可以使用这个功能:

def play():
    currentFrame.pack_forget()
    game.pack()