Python Forum
printing option menu variable in label in Tkinter
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
printing option menu variable in label in Tkinter
#1
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")
Reply
#2
This is solved. I needed to define 2 variables and use textvariable instead of text. stupid question. Sorry for being a noob.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Tkinter: An image and label are not appearing. emont 7 572 Mar-21-2024, 03:00 PM
Last Post: deanhystad
  pass a variable between tkinter and toplevel windows janeik 10 2,322 Jan-24-2024, 06:44 AM
Last Post: Liliana
  tkinter destroy label inside labelFrame Nick_tkinter 3 4,544 Sep-17-2023, 03:38 PM
Last Post: munirashraf9821
  [Tkinter] Updating Tkinter label using multiprocessing Agusms 6 3,131 Aug-15-2022, 07:10 PM
Last Post: menator01
  [Tkinter] The Text in the Label widget Tkinter cuts off the Long text in the view malmustafa 4 4,844 Jun-26-2022, 06:26 PM
Last Post: menator01
  [Tkinter] Update variable using tkinter entry methon drSlump 6 5,197 Oct-15-2021, 08:01 AM
Last Post: drSlump
  update text variable on label with keypress knoxvilles_joker 3 4,901 Apr-17-2021, 11:21 PM
Last Post: knoxvilles_joker
  [Tkinter] tkinter.Menu – How to make text-variable? Sir 3 5,630 Mar-10-2021, 04:21 PM
Last Post: Sir
  [Tkinter] tkinter global variable chalao_adda 6 11,027 Nov-14-2020, 05:37 AM
Last Post: chalao_adda
  tkinter: Image to Label Maryan 10 5,240 Oct-29-2020, 01:48 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020