Python Forum

Full Version: printing option menu variable in label in Tkinter
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi, I`m trying to print a variable from an option menu into a labels text string without effecting its value or the values of other variables and am stuck. I've been searching for answers but coming up empty handed. .get() doesn't seem to work for me. I am an idiot.
from tkinter import *
root = Tk()

# Make window 
root.geometry("900x600+400+50")
root.title('CRI CALCULATOR')
root.iconbitmap(root,default="cat.ico")
   
# Create a label as a child of root window
my_text = Label(root, text='WELCOME TO THE CONSTANT RATE INFUSION CALCULATOR')
my_text.config(font=('mincho', 15, 'bold'))
my_text.grid(columnspan=15,row=0,column=0)                
#weight label>>
Dog_weight = Label(root, text='What is the animals weight?')
Dog_weight.grid(columnspan=2,row=1,column=0,sticky=W)

#pound/kilogram drop down
variable = StringVar(root)
variable.set("Pounds") # default value
l1 = OptionMenu(root, variable, "Pounds", "Kilograms")
l1.grid(row=1,column=2,sticky=W)

#text entry
WEIGHT= Entry(root,width=10, bg="white")
WEIGHT.grid(row=1,column=3,sticky=W)

#dose label>>
dose = Label(root, text='What is dose prescribed in mg/ ')

per = Label(root, text='per')
dose.grid(columnspan=2,row=2,column=0,sticky=W)
per.grid(row=2,column=2,)

#dose hour/day
variable = StringVar(root)
variable.set("Hour") # default value
time = OptionMenu(root, variable, "Hour", "Day")
time.grid(row=2,column=3,sticky=W)


#text entry
DOSE2= Entry(root,width=10, bg="white")
DOSE2.grid(columnspan=3,row=2,column=4,sticky=W)

#drug mg/ml>>

drug_label = Label(root, text='How many mg are in a ml of this drug?')
drug_label.grid(columnspan=3,row=3,column=0,sticky=W)

#mg/ml text entry
mg= Entry(root,width=10, bg="white")
mg.grid(row=3,column=3,sticky=W)

#bag>>
bag1 = Label(root, text='How many ml of fluid are are in the bag')
bag1.grid(columnspan=4,row=4,column=0,sticky=W)

#bag text entry
bag= Entry(root,width=10, bg="white")
bag.grid(row=4,column=3,sticky=W)

#fluid_rate>>
fluid_rate1= Label(root, text='What is the animals current fluid rate in ml/hr')
fluid_rate1.grid(columnspan=4,row=5,column=0,sticky=W)

#fluid text entry
fluid_rate= Entry(root,width=10, bg="white")
fluid_rate.grid(row=5,column=3,sticky=W)

#maths
def ok():
    print ("value is")
    print (l1.get())

 button = Button(root, text="OK", command=ok)
button.grid(column=6)

mainloop()
The snippet of code I`m having trouble with is:
dose = Label(root, text='What is dose prescribed in mg/ ')
per = Label(root, text='per')

I need to retrieve the kilogram/pound variable so it reads: what is the dose prescribed in mg/(variable) per hour/day?

I had it working somehow but corrupted its variable into a label and was unretrievable for the maths equation.

Here is the working version I wrote I am trying to translate into GUI format to give it variables as some vets are prescribing for pounds/hours and kilograms/days or any combination of. Not getting paid btw complete not profit just helping out a friend and hobbying.

print("<<WELCOME TO THE CONSTANT RATE INFUSION CALCULATOR>>")
weight = float(input("How many pounds is the animal?"))
weight = weight / 2.2
print("that is", weight ,"Kilograms")
print(">")
dose = float(input("what is the dose per kilogram per day in mg?"))
dose = weight * dose
dose = dose / 24
print("that is", dose ,"mg/hour" )
print(">")
concentration = float(input("How many mg are in a ml of this drug?"))
dose = dose / concentration
print("that is", dose ,"mL per hour")
print(">")
bag = float(input("How many mL of fluid are in the bag?"))
print("thats interdasting")
rate = float(input("And what is the animals current fluid rate in ml/hr?"))
bag_hours = bag / rate
print(">")
print("that will last", bag_hours ,"hours of IV fluids per bag at this current rate")
print(">")
dose = bag_hours * dose
print("and you will need", dose ,"mL of drugs")
print("...to keep accurate remove", dose ,"mL fluid and add back in your drugs")
done = str(input("are you finished?"))
print ("ok bye")
This is solved. I needed to define 2 variables and use textvariable instead of text. stupid question. Sorry for being a noob.