获取tkinter条目小部件

时间:2018-04-25 03:04:22

标签: python function tkinter widget state

有没有办法获取条目小部件的状态当前状态?我以为我可以使用.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')

谢谢,

麦克

1 个答案:

答案 0 :(得分:0)

解决方案:widget["state"]

from tkinter import Tk, Entry

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

print(e["state"])

app.mainloop()
```