Tkinter:将现有内容添加到框架中

时间:2013-05-04 14:22:34

标签: python tkinter

我正在使用python / tkinter中的一个简单的基于文本的游戏,并进行了基本的设置。它显示了介绍。文本,然后您可以键入(这仍然是一项正在进行的工作)。但是,我意识到会有很多文本,并想到了框架的滚动条功能。

但是,我将当前内容添加到一个框架中,功能现在不起作用。我跟随某人的榜样,所以我不知道它是否正确。这是代码:

from Tkinter import *

w=Tk()
w.configure(background="navy")
w.iconbitmap(default="blankIcon.ico")
w.title("Decimated world")
w.resizable(0,0)

introText="When you wake, you see that the sky is dark; you can't tell the time of day."

scen1="You head toward the Town hall."

class App(Frame):
def __init__(self,w):
    Frame.__init__(self,w)
def key(event):
    print event.char

t=Text(w)
t.insert(INSERT,introText)
t.configure(state=DISABLED,background="navy",foreground="white")
t.pack()

def do_command(command):
    t.configure(state=NORMAL)
    t.insert(INSERT,'\n>>> {}'.format(command))
    t.configure(state=DISABLED)

s=StringVar()
e=Entry()
e.configure(background="navy",foreground="white")
e.focus_set()
e.pack(side=BOTTOM)

def enter(event):
    do_command(e.get())
    if e.get()=="walk north":
        t.configure(state=NORMAL)
        t.insert(INSERT,"\n"+scen1)
        t.configure(state=DISABLED)

e.bind("<KeyRelease-Return>",enter)


w.mainloop()

我很感激任何人帮助将现有的功能/小部件放入框架中。感谢。

1 个答案:

答案 0 :(得分:0)

只要文本有名称,您就可以使用pack和unpack方法。

例如:

b = Label(root, text='Click Me')
b.pack()

然后删除它:

b.pack_forget()

然后当您想再次添加时,只需输入:

b.pack()

就这么简单!

相关问题