![](/img/trans.png)
[英]How can I set the focus to a widget (that isn't an entry) by clicking on it?
[英]How do I set focus to an entry widget from a function
我有一個包含多個函數的類,其中一個拋出異常,我想將焦點設置回它正在驗證的條目小部件。 電話:
self.entryWidget.set_focus()
返回一個AttributeError:
'App' object has no attribute 'entryWidget'
我怎么能在__init__
之外引用這個小部件?
class App:
def __init__(self,master):
calcframe = Frame(master)
calcframe.pack()
self.vol = DoubleVar()
entryWidget = Entry(calcframe, textvariable=self.vol)
entryWidget.grid(row=1, column=1, sticky=W)
entryWidget.focus()
def updateSIP(self):
try:
volume = self.vol.get()
except:
self.entryWidget.set_focus()
root = Tk()
root.wm_title('title')
app = App(root)
root.mainloop()
問題是你沒有使entryWidget
成為App
一個屬性。
要做到這一點,放置self.
在它之前:
def __init__(self,master):
calcframe = Frame(master)
calcframe.pack()
self.vol = DoubleVar()
self.entryWidget = Entry(calcframe, textvariable=self.vol)
self.entryWidget.grid(row=1, column=1, sticky=W)
self.entryWidget.focus()
現在, entryWidget
是App
一個屬性,可以通過self
訪問。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.