问题描述
限时送ChatGPT账号..考虑以下代码:
import tkinter as tk
FONT="Arial 20 bold"
app = tk.Tk()
tk.Entry(app,text="hi", font=FONT).pack()
tk.Entry(app,text="hi", font=FONT).pack()
app.mainloop()
此动画显示,文本内容相互关联:一个条目的更改会自动更新另一个条目.请注意,文本选项不绑定到任何 stringVar 对象,而是绑定到字符串文字.
This animation shows, that the text contents are connected one to the other: change in one entry automatically updates the other one. Notice that text options are not bounded to any stringVar object but to a string literal.
奇怪的是,如果文本选项被初始化为不同的文字字符串,例如hi and Hi",条目内容变得不相关.
Curiously, if text options are initialized to distinct litteral string, for instance "hi and Hi", entry contents become unrelated.
这是预期的行为吗?
在三个条目中,两个具有相同文本的条目也链接在一起:
With three entries, the two that have the same text are also linked:
import tkinter as tk
FONT="Arial 20 bold"
app = tk.Tk()
tk.Entry(app, text="hi", font=FONT).pack()
tk.Entry(app, text="there", font=FONT).pack()
tk.Entry(app, text="hi", font=FONT).pack()
app.mainloop()
即使这些条目被分配给不同的变量:
Even if these entries are assigned to distinct variables:
import tkinter as tk
FONT="Arial 20 bold"
app = tk.Tk()
a = tk.Entry(app, text="hi", font=FONT).pack()
b = tk.Entry(app, text="there", font=FONT).pack()
c = tk.Entry(app, text="hi", font=FONT).pack()
app.mainloop()
推荐答案
问题实际上出在 text
参数上.通过传递 text
,您正在创建 Entry
小部件的 textvariable
:
The problem lies with the text
parameter actually. By passing text
you are creating a textvariable
of the Entry
widget:
import tkinter as tk
FONT="Arial 20 bold"
app = tk.Tk()
a = tk.Entry(app, text="hi", font=FONT)
b = tk.Entry(app, text="there", font=FONT)
c = tk.Entry(app, text="hi", font=FONT)
for i in (a,b,c):
i.pack()
print (i["textvariable"])
app.mainloop()
要解决此问题 - 只需不要将 text
作为参数传递.我认为无论如何你都不需要它.
To fix this problem - simply don't pass text
as a parameter. I don't think you need it anyways.
这篇关于输入文本选项:好奇行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论