I'm trying to create a GUI and I am having trouble getting read only entry fields to insert a value. I've tried using a text value and this didn't insert anything into the field.
here is the 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.
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. 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.
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.
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.