我问了一个关于将文本插入到两个输入框中的问题,而该文本只应插入一个输入框中。问题代码行如下:
MoneyAvailableTextBox = Entry(temp, font=('arial', 14, 'bold'), bg='White', fg = 'Black', bd = 5, width = 10, borderwidth = 4, justify = CENTER, text = '£')
MoneyAvailableTextBox.grid(row = 1, column = 0, pady = 10)
HeistAwardTextBox = Entry(temp, font=('arial', 14, 'bold'), bg='White', fg = 'Black', bd = 5, width = 10, borderwidth = 4, justify = CENTER, text = '£')
HeistAwardTextBox.grid(row = 3, column = 0, pady = 10)
我的一个 friend 最终弄清楚了问题所在,我回答了自己的问题,以防其他人遇到这个问题。问题是两个输入框都有 text = '£'
。我的 friend 只是将其中一个更改为值 $
,问题就解决了。删除它们也可以解决问题。他和我都不确定为什么必须使用相同的 text = '£'
输入框会使它们被视为相同的输入框。
我已经在下面复制了这个问题。我简化了代码。
import tkinter as tk
from tkinter import*
trialGUI = Tk()
trialGUI.title('Text Boxes')
#This is the text which will be inserted
value1 = 'Hello'
value2 = 'Bye'
#This inserts the text into the entry boxes
def updateStats():
entryBox1.delete('0', END)
#This should insert Hello in the first box
entryBox1.insert(tk.INSERT, value1)
entryBox2.delete('0', END)
#This should insert Bye in the second box
entryBox2.insert(tk.INSERT, value2)
# These are the text boxes
entryBox1 = Entry(trialGUI, text = '£')
entryBox1.grid(row = 0)
entryBox2 = Entry(trialGUI, text = '£')
entryBox2.grid(row = 1)
#Button which when pressed inserts texting into the entry boxes
Button1 = Button(trialGUI, command = updateStats, text = 'Insert Text')
Button1.grid(row = 2)
trialGUI.mainloop()
我说过,这个问题已经解决了。我只是在寻找问题发生的原因的解释。
最佳答案
text
选项与 Entry
小部件的 textvariable
选项相同。由于您向其传递了一个字符串 "£"
,而不是 StringVar
的实例,因此将使用该字符串创建一个 StringVar
的实例隐含地为您命名。因此,两个 Entry
小部件都使用相同的 StringVar
。因此,更改其中一个也会更改另一个,因为它们共享相同的 StringVar
。
关于python - 为什么具有相同 'text =' 值的两个输入框被视为相同的输入框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66007375/