英文:
Weird interaction of cget() in tkinter.ttk
问题
在使用tkinter玩耍时,我偶然遇到了这个问题。显然,ttk.Entry在打印状态之前似乎无法识别已配置的状态。我的代码如下:
import tkinter as tk
from tkinter import ttk
def on_focus_in(entry):
if entry['state'] == 'disabled': #请注意这个代码块
entry.configure(state='normal')
entry.delete(0, 'end')
print('1')
print(entry_x.cget("state"))
if entry.cget('state') == 'disabled':
entry.configure(state='normal')
entry.delete(0, 'end')
print('2')
def on_focus_out(entry, placeholder):
if entry.get() == "":
entry.insert(0, placeholder)
entry.configure(state='disabled')
root = tk.Tk()
entry_x = ttk.Entry(root, font=('Arial', 13), width=50)
entry_x.pack(pady=10, ipady=7)
entry_x.insert(0, "占位符X")
entry_x.configure(state='disabled')
entry_y = tk.Entry(root, width=50, font=('Arial', 13))
entry_y.pack(pady=10, ipady=7)
entry_y.insert(0, "占位符Y")
entry_y.configure(state='disabled')
x_focus_in = entry_x.bind('<Button-1>', lambda x: on_focus_in(entry_x))
x_focus_out = entry_x.bind(
'<FocusOut>', lambda x: on_focus_out(entry_x, '占位符X'))
y_focus_in = entry_y.bind('<Button-1>', lambda x: on_focus_in(entry_y))
y_focus_out = entry_y.bind(
'<FocusOut>', lambda x: on_focus_out(entry_y, '占位符Y'))
root.mainloop()
有人可以解释为什么会出现这种情况吗?
显然,当我点击X输入框时,日志返回"2",这意味着应该打印"1"的代码块未运行。Y输入框正常工作。
英文:
Been playing out with tkinter until I stumble upon this. Apparently, it seems that the ttk.Entry does not recognize the configured state until the state is printed.
My code is as follow:
import tkinter as tk
from tkinter import ttk
def on_focus_in(entry):
if entry['state'] == 'disabled': #pay attention to this code block
entry.configure(state='normal')
entry.delete(0, 'end')
print('1')
print(entry_x.cget("state"))
if entry.cget('state') == 'disabled':
entry.configure(state='normal')
entry.delete(0, 'end')
print('2')
def on_focus_out(entry, placeholder):
if entry.get() == "":
entry.insert(0, placeholder)
entry.configure(state='disabled')
root = tk.Tk()
entry_x = ttk.Entry(root, font=('Arial', 13), width=50)
entry_x.pack(pady=10, ipady=7)
entry_x.insert(0, "Place Holder X")
entry_x.configure(state='disabled')
entry_y = tk.Entry(root, width=50, font=('Arial', 13))
entry_y.pack(pady=10, ipady = 7)
entry_y.insert(0, "Place Holder Y")
entry_y.configure(state='disabled')
x_focus_in = entry_x.bind('<Button-1>', lambda x: on_focus_in(entry_x))
x_focus_out = entry_x.bind(
'<FocusOut>', lambda x: on_focus_out(entry_x, 'Place Holder X'))
y_focus_in = entry_y.bind('<Button-1>', lambda x: on_focus_in(entry_y))
y_focus_out = entry_y.bind(
'<FocusOut>', lambda x: on_focus_out(entry_y, 'Place Holder Y'))
root.mainloop()
Can anyone explain to me why is this the case?
Apparently, when I click the X entry, the log returns "2", which means that the code block that should print "1" does not run. The Y entry works fine.
答案1
得分: 0
问题的根本在于 entry['state']
不返回一个字符串,而是返回一个类型为 <class '_tkinter.Tcl_Obj'>
的对象。您正在将其与一个字符串进行比较,因此条件将始终为假。您可以在您的 if
语句中将其强制转换为字符串,或使用 string
属性:
if str(entry['state']) == 'disabled':
或
if entry['state'].string == 'disabled':
英文:
The root of the problem is that entry['state']
does not return a string. Instead, it returns an object of type <class '_tkinter.Tcl_Obj'>
. You're comparing it to a string, so the condition will always be false. You can cast it to the string in your if
statement or use the string
attribute:
if str(entry['state']) == 'disabled':
or
if entry['state'].string == 'disabled':
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论