为什么具有相同 'text =' 值的两个输入框被视为相同的输入框?
Why are two Entry boxes with the same 'text =' values treated as the same entry box?
我问过一个关于文本被插入到两个输入框中的问题,而它应该只被插入到一个输入框中。问题代码行原来是这些:
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)
我的一个朋友最终弄清楚了问题所在,我回答了我自己的问题,以防其他人遇到这个问题。问题是两个输入框都有 text = '£'
。我的朋友只是将其中一个更改为具有 $
的值,问题就解决了。删除它们也可以解决问题。他和我都不确定为什么必须使用相同 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
.
我问过一个关于文本被插入到两个输入框中的问题,而它应该只被插入到一个输入框中。问题代码行原来是这些:
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)
我的一个朋友最终弄清楚了问题所在,我回答了我自己的问题,以防其他人遇到这个问题。问题是两个输入框都有 text = '£'
。我的朋友只是将其中一个更改为具有 $
的值,问题就解决了。删除它们也可以解决问题。他和我都不确定为什么必须使用相同 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
.