接收来自Python的输入';s tkinter条目小部件并将其显示在标签中
我正在使用的程序当前需要用户的输入显示在“程序”窗口中。我研究了互联网和stackoverflow,发现了几种解决我问题的方法,但似乎都不管用。我的目标是通过Python的tkinter entry小部件接收用户的输入,并在一个新标签中显示结果,同时取出我的初始标签和输入框,然而,程序拒绝了我的回答尝试 为了实现我的目标,您有什么策略、代码行/库或建议 我现有的解决方案:接收来自Python的输入';s tkinter条目小部件并将其显示在标签中,python,tkinter,label,python-2.6,tkinter-entry,Python,Tkinter,Label,Python 2.6,Tkinter Entry,我正在使用的程序当前需要用户的输入显示在“程序”窗口中。我研究了互联网和stackoverflow,发现了几种解决我问题的方法,但似乎都不管用。我的目标是通过Python的tkinter entry小部件接收用户的输入,并在一个新标签中显示结果,同时取出我的初始标签和输入框,然而,程序拒绝了我的回答尝试 为了实现我的目标,您有什么策略、代码行/库或建议 我现有的解决方案: .get() textvariable=self.entdat 现行守则如下: from Tkinter import *
.get()
textvariable=self.entdat
现行守则如下:
from Tkinter import *
import time
class Input(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, background="white")
self.parent = parent
self.initUI()
self.entdat = StringVar
self.timestr = StringVar()
self.makeWidgets()
def makeWidgets(self):
self.ol = Label(text="Objective:")
self.ol.pack(side=TOP)
self.ew = Entry()
self.ew.pack(side=TOP)
self.b = Button(text="OK", command=self.clicked)
self.b.pack(side=TOP)
def clicked(self):
self.entdat = self.ew.get()
self.dat = Label(textvariable=self.ew.get())
self.dat.pack(side=TOP)
self.hide_Widget()
def hide_Widget(event):
event.ew.pack_forget()
event.ol.pack_forget()
event.b.pack_forget()
def main():
root = Tk()
root.geometry("240x135+25+50")
tm = Input(root)
tm.pack(side=TOP)
root.mainloop()
if __name__ == '__main__':
main()
我修改了你的代码,这样它至少可以执行,希望能以你想要的方式执行
from Tkinter import *
class Input(Frame):
def __init__(self, parent=None, **kw):
Frame.__init__(self, parent, background="white")
self.parent = parent
self.entdat = StringVar()
self.makeWidgets()
def makeWidgets(self):
self.ol = Label(text="Objective:")
self.ol.pack(side=TOP)
self.ew = Entry(textvariable=self.entdat)
self.ew.pack(side=TOP)
self.b = Button(text="OK", command=self.clicked)
self.b.pack(side=TOP)
def clicked(self):
self.dat = Label(self, textvariable=self.entdat )
self.dat.pack(side=TOP)
self.distroy_Widget()
def distroy_Widget(self):
self.ew.destroy()
self.ol.destroy()
self.b.destroy()
def main():
root = Tk()
root.geometry("240x135+25+50")
tm = Input(root)
tm.pack(side=TOP)
root.mainloop()
if __name__ == '__main__':
main()
希望有帮助。什么是
self.initUI()
?您提供的代码中没有定义它。很抱歉。我只包括程序中包含问题的部分。self.intitUI()将parent.title设置为“Input”。非常感谢,这正是我想要的。这对我来说很重要,你会花时间做这件事。现在,我又回到编程上来了!:-)@瑞克:不用担心。很高兴我能帮忙。