【问题标题】:Obtaining the state of a tkinter entry widget获取 tkinter 条目小部件的状态
【发布时间】:2023-03-22 18:24:01
【问题描述】:

有没有办法获取入口小部件的状态当前状态?我以为我可以使用 .winfo 方法,但似乎并非如此?尝试创建一个功能/按钮,根据条目小部件的状态进行不同的操作...到目前为止,没有运气...有什么想法吗?

Entry 和 Button 小部件:

    self.contact_phone = tk.Entry(contact_frame, font='arial 10 bold', relief='flat', bg='grey60', textvariable=self.var3, state='readonly', readonlybackground='grey60')
    self.contact_phone.place(anchor='nw', y=50)

    self.change_phone = tk.Button(contact_frame, text='Change', font='arial 10 bold', bd=2, bg='grey80', command=self.changephone)
    self.change_phone.place(anchor='nw', y=48, x=180)

建议的功能(为简洁起见,省略了打开/写入文件内容):

def changephone(self):
    if #the state of the widget is readonly:
        '''config the widget to allow entry'''
        self.contact_phone.config(state='normal', relief='sunken', bg='white', bd=2)
        '''change the text on the button to reflect the other functionality'''
        self.change_phone.config(text='Apply')
    elif #the state of the widget is normal:
        '''get the new contents and write it to a file'''
        newnumber = self.contact_phone.get()
        #open a file and write the new number to it...
        '''return the widget and button to their previous states'''
        self.contact_phone.config(state='readonly', relief='sunken', readonlybackground='grey60')
        self.change_phone.config(text='Change')

谢谢,

迈克

【问题讨论】:

  • 阅读cget 方法。
  • 太棒了!谢谢!完全忘记了它的存在……

标签: python function tkinter widget state


【解决方案1】:

解决方案:widget["state"]

from tkinter import Tk, Entry

app = Tk()
e = Entry(app,state="readonly")
e.pack()

print(e["state"])

app.mainloop()
```

【讨论】:

    猜你喜欢
    • 2022-12-08
    • 1970-01-01
    • 2023-03-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-15
    • 1970-01-01
    • 1970-01-01
    • 2020-02-23
    相关资源
    最近更新 更多