您可以使用dir()
來查看哪些方法和屬性具有對象。
print(dir(self.combobox['state']))
結果
['__class__', '__cmp__', '__delattr__', '__doc__', '__format__',
'__getattribute__', '__hash__', '__init__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__',
'__subclasshook__', '__unicode__', 'string', 'typename']
你可以看到string
(方法或屬性)
如果檢查
print(self.combobox['state'].string == tk.NORMAL)
你True
str()
工程太
print(str(self.combobox['state']) == tk.NORMAL)
編輯:測試最小的工作,例如:
try:
# Python 2
import Tkinter as tk
import ttk
except:
# Python 3
import tkinter as tk
import tkinter.ttk as ttk
root = tk.Tk()
c = ttk.Combobox(root)
c.pack()
print(c['state'], c['state'] == tk.NORMAL)
print('normal:', c['state'].string == tk.NORMAL, str(c['state']) == tk.NORMAL)
print('disabled:', c['state'].string == tk.DISABLED, str(c['state']) == tk.DISABLED)
c['state'] = tk.DISABLED
print('normal:', c['state'].string == tk.NORMAL, str(c['state']) == tk.NORMAL)
print('disabled:', c['state'].string == tk.DISABLED, str(c['state']) == tk.DISABLED)
root.mainloop()
'self.combobox [ '狀態'] string' – furas