Python Tkinter:将现有内容添加到框架
我正在用python/tkinter开发一个简单的基于文本的游戏,到目前为止我已经做了一个基本的设置。它显示介绍。文本,然后您可以键入(这仍然是一项正在进行的工作)。然而,我意识到将会有很多文本,并且想到了框架的滚动条功能 但是,我将当前内容添加到了一个框架中,这些功能现在无法工作。我在效仿别人的榜样,所以我不知道这是否正确。代码如下:Python Tkinter:将现有内容添加到框架,python,tkinter,Python,Tkinter,我正在用python/tkinter开发一个简单的基于文本的游戏,到目前为止我已经做了一个基本的设置。它显示介绍。文本,然后您可以键入(这仍然是一项正在进行的工作)。然而,我意识到将会有很多文本,并且想到了框架的滚动条功能 但是,我将当前内容添加到了一个框架中,这些功能现在无法工作。我在效仿别人的榜样,所以我不知道这是否正确。代码如下: from Tkinter import * w=Tk() w.configure(background="navy") w.iconbitmap(defaul
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()
从Tkinter导入*
w=Tk()
w、 配置(background=“navy”)
w、 iconbitmap(默认值=“blankIcon.ico”)
w、 标题(“毁灭世界”)
w、 可调整大小(0,0)
introText=“当你醒来时,你看到天空是黑暗的;你不知道一天的时间。”
scene1=“你朝市政厅走去。”
类应用程序(框架):
定义初始化(self,w):
帧。\uuuu初始化(self,w)
def键(事件):
打印事件.char
t=文本(w)
t、 插入(插入,插入文本)
t、 配置(状态=禁用,背景=“海军”,前景=“白色”)
t、 包()
def do_命令(命令):
t、 配置(状态=正常)
t、 插入(插入,'\n>>>{}'。格式(命令))
t、 配置(状态=已禁用)
s=StringVar()
e=条目()
e、 配置(背景=“海军”,前景=“白色”)
e、 焦点集()
e、 包装(侧面=底部)
def enter(事件):
do_命令(例如get())
如果e.get()=“向北走”:
t、 配置(状态=正常)
t、 插入(插入“\n”+scen1)
t、 配置(状态=已禁用)
e、 绑定(“,输入)
w、 mainloop()
如果有人能帮助我将现有的函数/小部件放到一个框架中,我将不胜感激。谢谢。只要文本有名称,您就可以使用打包和解包方法 例如:
b = Label(root, text='Click Me')
b.pack()
然后要删除它:
b.pack_forget()
然后,如果要再次添加,只需键入:
b.pack()
就这么简单 示例代码中的缩进不正确。由于python高度依赖缩进,在修复缩进之前,我们无法确定您的意图。您所说的“函数现在不工作”是什么意思?您能否更具体一些(错误消息、预期行为等)?