[英]tkinter entry fields not updating using .insert
I'm trying to create a GUI and I am having trouble getting read only entry fields to insert a value.我正在尝试创建一个 GUI,但在获取只读输入字段以插入值时遇到了麻烦。 I've tried using a text value and this didn't insert anything into the field.我试过使用文本值,但这并没有在字段中插入任何内容。
here is the function:这是 function:
def calculateFinalCMD():
global planetInfo
global getDestinationNum
displayfc1.grid(row=14, column=2)
displayfc1.insert(0, ((100 - int(crewInfo[0][1])) * planetInfo[getDestinationNum][1]))
displayfc2.grid(row=15, column=2)
displayfc2.insert(0, ((100 - int(crewInfo[1][1])) * planetInfo[getDestinationNum][1]))
displayfc3.grid(row=16, column=2)
displayfc3.insert(0, ((100 - int(crewInfo[2][1])) * planetInfo[getDestinationNum][1]))
displayfc4.grid(row=17, column=2)
displayfc4.insert(0, ((100 - int(crewInfo[3][1])) * planetInfo[getDestinationNum][1]))
displayms1.grid(row=18, column=2)
displayms1.insert(0, ((150 - int(crewInfo[4][1])) * planetInfo[getDestinationNum][1]))
displayms2.grid(row=19, column=2)
displayms2.insert(0, ((150 - int(crewInfo[5][1])) * planetInfo[getDestinationNum][1]))
Here are the entry fields which are separate from the function, getDestinationNum is updated by another entry field earlier in the code.这是与 function 不同的输入字段,getDestinationNum 由代码前面的另一个输入字段更新。
getDestinationNum = 0
displayfc1 = Entry(root, state="readonly")
displayfc2 = Entry(root, state="readonly")
displayfc3 = Entry(root, state="readonly")
displayfc4 = Entry(root, state="readonly")
displayms1 = Entry(root, state="readonly")
displayms2 = Entry(root, state="readonly")
Any ideas on how anything could be changed or if the code is wrong?关于如何更改任何内容或代码是否错误的任何想法? thanks!谢谢!
This is not a minimal reproducible example.这不是一个最小的可重现示例。 Something like this would have better explained your problem.这样的事情会更好地解释你的问题。
from tkinter import Tk, Entry
root = Tk()
my_entry = Entry(root, state='readonly')
my_entry.grid(row=0, column=0)
my_entry.insert(0, 500) # does not work
root.mainloop()
The problem is that your Entry
widget is read only and cannot be updated.问题是您的Entry
小部件是只读的,无法更新。 We can get around that by waiting until we have set a value and use the configure
method to update the state after the fact.我们可以通过等到我们设置一个值并在事后使用configure
方法更新 state 来解决这个问题。
from tkinter import Tk, Entry
root = Tk()
my_entry = Entry(root)
my_entry.grid(row=0, column=0)
my_entry.insert(0, 500)
my_entry.configure(state='readonly')
root.mainloop()
There are some other things that could be improved with your code like relegating your calculations to a helper function, having your entries in arrays, and setting your entries in a loop.您的代码还可以改进其他一些事情,例如将您的计算委托给帮助程序 function,在 arrays 中输入您的条目,以及在循环中设置您的条目。
This is just an example as I don't have access to what's inside your variables.这只是一个示例,因为我无法访问变量中的内容。
# This makes it much easier to follow the logic
# of your calculation
def calculate(multiplier, crew, planet):
return (multiplier - int(crew[1])) * planet[1]
multipliers = [100, 100, 100, 100, 150, 150]
# you could just append your entries to an array when creating them
# rather than add them here
entries = [displayfc1,
displayfc2,
displayfc3,
displayfc4,
displayms1,
displayms2
]
# zipping them up allows you to iterate through all
# of them at once
for entry, mult, crew, row in zip(entries,
multipliers,
crewInfo,
range(14, 20)
):
entry.grid(row=row, column=2)
entry.insert(0, calculate(mult, crew, planetInfo[getDestinationNum]))
entry.configure(state='readonly')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.