2013-05-04 64 views
1

我正在研究一个基于python/tkinter的基于文本的简单游戏,并进行了基本设置。它显示了介绍。文本,然后你可以键入(这仍然是一个正在进行的工作)。但是,我意识到将会有很多文本,并且考虑到框架的滚动条功能。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

您的缩进在您的示例代码中不正确。由于python高度依赖缩进,因此在修复缩进之前,我们无法确定您想要的内容。 – 2013-05-04 16:01:32

+0

你的意思是“这些功能现在不能工作”是什么意思?你能更具体(错误信息,预期的行为等)? – 2013-05-05 01:37:06

回答

0

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

例如:

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

,然后将其删除:

b.pack_forget() 

,然后当你想再次添加它只需键入:

b.pack() 

它就是这么简单那样!