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

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

Weird interaction of cget() in tkinter.ttk

问题

在使用tkinter玩耍时,我偶然遇到了这个问题。显然,ttk.Entry在打印状态之前似乎无法识别已配置的状态。我的代码如下:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. def on_focus_in(entry):
  4. if entry['state'] == 'disabled': #请注意这个代码块
  5. entry.configure(state='normal')
  6. entry.delete(0, 'end')
  7. print('1')
  8. print(entry_x.cget("state"))
  9. if entry.cget('state') == 'disabled':
  10. entry.configure(state='normal')
  11. entry.delete(0, 'end')
  12. print('2')
  13. def on_focus_out(entry, placeholder):
  14. if entry.get() == "":
  15. entry.insert(0, placeholder)
  16. entry.configure(state='disabled')
  17. root = tk.Tk()
  18. entry_x = ttk.Entry(root, font=('Arial', 13), width=50)
  19. entry_x.pack(pady=10, ipady=7)
  20. entry_x.insert(0, "占位符X")
  21. entry_x.configure(state='disabled')
  22. entry_y = tk.Entry(root, width=50, font=('Arial', 13))
  23. entry_y.pack(pady=10, ipady=7)
  24. entry_y.insert(0, "占位符Y")
  25. entry_y.configure(state='disabled')
  26. x_focus_in = entry_x.bind('<Button-1>', lambda x: on_focus_in(entry_x))
  27. x_focus_out = entry_x.bind(
  28. '<FocusOut>', lambda x: on_focus_out(entry_x, '占位符X'))
  29. y_focus_in = entry_y.bind('<Button-1>', lambda x: on_focus_in(entry_y))
  30. y_focus_out = entry_y.bind(
  31. '<FocusOut>', lambda x: on_focus_out(entry_y, '占位符Y'))
  32. 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:

  1. import tkinter as tk
  2. from tkinter import ttk
  3. def on_focus_in(entry):
  4. if entry[&#39;state&#39;] == &#39;disabled&#39;: #pay attention to this code block
  5. entry.configure(state=&#39;normal&#39;)
  6. entry.delete(0, &#39;end&#39;)
  7. print(&#39;1&#39;)
  8. print(entry_x.cget(&quot;state&quot;))
  9. if entry.cget(&#39;state&#39;) == &#39;disabled&#39;:
  10. entry.configure(state=&#39;normal&#39;)
  11. entry.delete(0, &#39;end&#39;)
  12. print(&#39;2&#39;)
  13. def on_focus_out(entry, placeholder):
  14. if entry.get() == &quot;&quot;:
  15. entry.insert(0, placeholder)
  16. entry.configure(state=&#39;disabled&#39;)
  17. root = tk.Tk()
  18. entry_x = ttk.Entry(root, font=(&#39;Arial&#39;, 13), width=50)
  19. entry_x.pack(pady=10, ipady=7)
  20. entry_x.insert(0, &quot;Place Holder X&quot;)
  21. entry_x.configure(state=&#39;disabled&#39;)
  22. entry_y = tk.Entry(root, width=50, font=(&#39;Arial&#39;, 13))
  23. entry_y.pack(pady=10, ipady = 7)
  24. entry_y.insert(0, &quot;Place Holder Y&quot;)
  25. entry_y.configure(state=&#39;disabled&#39;)
  26. x_focus_in = entry_x.bind(&#39;&lt;Button-1&gt;&#39;, lambda x: on_focus_in(entry_x))
  27. x_focus_out = entry_x.bind(
  28. &#39;&lt;FocusOut&gt;&#39;, lambda x: on_focus_out(entry_x, &#39;Place Holder X&#39;))
  29. y_focus_in = entry_y.bind(&#39;&lt;Button-1&gt;&#39;, lambda x: on_focus_in(entry_y))
  30. y_focus_out = entry_y.bind(
  31. &#39;&lt;FocusOut&gt;&#39;, lambda x: on_focus_out(entry_y, &#39;Place Holder Y&#39;))
  32. 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 属性:

  1. if str(entry['state']) == 'disabled':

  1. 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:

  1. if str(entry[&#39;state&#39;]) == &#39;disabled&#39;:

or

  1. 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:

确定