当我按下python窗体上的按钮时,我想添加新标签并将其放在网格上。当我按下按钮时,什么也没发生。你知道吗
我的代码随附:
from Tkinter import *
class Application(Frame):
def __init__(self,master):
Frame.__init__(self,master)
self.grid()
self.create_widgets()
def create_widgets(self):
topiclbl = Label(self,text = 'Enter topic for search',font=(12))
topiclbl.grid()
topictxt = Text(self,height=1, width=30,font=(12))
topictxt.grid()
searchbtn = Button(self,text = 'Search Videos',command='search')
searchbtn.grid()
def search(self):
message = 'Searching...'
self.topictxt.insert(0.0,message)
searchlbl = Label(self,text = message,font=(12))
searchlbl.grid()
root = Tk()
root.title('Video Search')
root.geometry('600x600')
app=Application(root)
root.mainloop()
代码中有几个问题:
您在
command
参数中引用了错误的方法名,应该是:searchbtn = Button(self,text = 'Search Videos',command=self.search)
另外,您还有一些属性问题:
不能在此方法之外访问在
create_widget
方法中定义的topictxt
,除非将其作为实例属性,方法如下:self.topiclbl = Label(self,text = 'Enter topic for search',font=(12))
。。。其余的也一样。为了解决这个问题:另一种方法是使用
lambda
传递所需的对象(标签):创建函数时,应该将实际函数传递给
Button
例如
相关问题 更多 >
编程相关推荐