tkinter.ttk中cget()的奇怪互动

huangapple go评论61阅读模式
英文:

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[&#39;state&#39;] == &#39;disabled&#39;: #pay attention to this code block
        entry.configure(state=&#39;normal&#39;)
        entry.delete(0, &#39;end&#39;)
        print(&#39;1&#39;)

    print(entry_x.cget(&quot;state&quot;))
    if entry.cget(&#39;state&#39;) == &#39;disabled&#39;:
        entry.configure(state=&#39;normal&#39;)
        entry.delete(0, &#39;end&#39;)
        print(&#39;2&#39;)

def on_focus_out(entry, placeholder):
    if entry.get() == &quot;&quot;:
        entry.insert(0, placeholder)
        entry.configure(state=&#39;disabled&#39;)


root = tk.Tk()

entry_x = ttk.Entry(root, font=(&#39;Arial&#39;, 13), width=50)
entry_x.pack(pady=10, ipady=7)
entry_x.insert(0, &quot;Place Holder X&quot;)
entry_x.configure(state=&#39;disabled&#39;)

entry_y = tk.Entry(root, width=50, font=(&#39;Arial&#39;, 13))
entry_y.pack(pady=10, ipady = 7)
entry_y.insert(0, &quot;Place Holder Y&quot;)
entry_y.configure(state=&#39;disabled&#39;)

x_focus_in = entry_x.bind(&#39;&lt;Button-1&gt;&#39;, lambda x: on_focus_in(entry_x))
x_focus_out = entry_x.bind(
    &#39;&lt;FocusOut&gt;&#39;, lambda x: on_focus_out(entry_x, &#39;Place Holder X&#39;))

y_focus_in = entry_y.bind(&#39;&lt;Button-1&gt;&#39;, lambda x: on_focus_in(entry_y))
y_focus_out = entry_y.bind(
    &#39;&lt;FocusOut&gt;&#39;, lambda x: on_focus_out(entry_y, &#39;Place Holder Y&#39;))

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[&#39;state&#39;] does not return a string. Instead, it returns an object of type &lt;class &#39;_tkinter.Tcl_Obj&#39;&gt;. 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[&#39;state&#39;]) == &#39;disabled&#39;: 

or

if entry[&#39;state&#39;].string == &#39;disabled&#39;: 

huangapple
  • 本文由 发表于 2023年7月13日 11:14:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/76675658.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定