0

Tkinter. How do i show a value of a code to the user after he presses a button???

i did a code to do some mat calculation . the user enters 2 values and then he presses button . the code will take that 2 values do some math and return a result . how do i show that result to the user ?

11th Feb 2017, 1:43 AM
el.hetch
el.hetch - avatar
8 Answers
+ 2
When creating your tk.Button() object pass in command=func_name and in that function update your labels text. Or in the button.config() from tkinter import * bye = 'Goodbye' def update_text(): label.config(text=bye) root = Tk() root.title("My Title") label = Label(root, text="Hello") label.pack() button = Button(root, text="Click Me", width=30, command=update_text) button.pack() root.mainloop()
11th Feb 2017, 3:12 AM
ChaoticDawg
ChaoticDawg - avatar
+ 1
ah I see.. when I do that, I use a label.. I set the text of the label to a variable and when the button is pressed I update that variable, thus updating the label on screen
11th Feb 2017, 2:37 AM
LordHill
LordHill - avatar
+ 1
import math from Tkinter import* def circle_weight (): area =math.pi*int (ent1.get ())**2 weight = area *int (ent2.get ()) print weight master =Tk () master.title ("Disc weight calculator") master.geometry ("300x300") master.wm_iconbitmap ('steel-alloy.ico') lbl1=Label (master,text="radius",bg="cornsilk") ent1=Entry(master) lbl1.pack () ent1.pack () lbl2=Label (master,text="thk") ent2=Entry (master) lbl2.pack () ent2.pack () btn=Button(master,text=Calculate",command=circle_weight) btn.pack () var=StringVar () lbl3=Label (master,textvariable=var) var.set (circle_weight) lbl3.pack () master.mainloop () that is my code still not running . sorry i am dump :)
11th Feb 2017, 3:22 AM
el.hetch
el.hetch - avatar
+ 1
import math from tkinter import * def circle_weight(): area = math.pi * int(ent1.get()) ** 2 weight = area * int(ent2.get()) var.set(weight) master = Tk() master.title("Disc weight calculator") master.geometry("300x300") master.wm_iconbitmap('steel-alloy.ico') lbl1 = Label(master, text="radius", bg="cornsilk") lbl1.pack() ent1 = Entry(master) ent1.pack() lbl2 = Label(master, text="thk") lbl2.pack() ent2 = Entry(master) ent2.pack() btn = Button(master, text="Calculate", command=circle_weight) btn.pack() var = StringVar() var.set("Output") # <-- Set to the initial String that you want the label to have lbl3 = Label(master, textvariable=var) lbl3.pack() master.mainloop() You just have a couple of typos. Your missing a " for your button text -> Calculate" and the next line you have dtn instead of btn. And add a space between import and *. Note this is for python 3.5 and organized for readability. python 3.5 import is tkinter for 2.7 Tkinter
11th Feb 2017, 3:49 AM
ChaoticDawg
ChaoticDawg - avatar
0
sorry forgot to mention . i want to do that in tkinter
11th Feb 2017, 2:33 AM
el.hetch
el.hetch - avatar
0
lbl3=Label (master,textvariable=??????) lbl3.pack () if this what you mean . so i dont know what to put in the question mark place. the calculation is been made under def .....() but when i call the def name it return nothing.
11th Feb 2017, 2:40 AM
el.hetch
el.hetch - avatar
0
sorry i am just a baby in coding
11th Feb 2017, 2:41 AM
el.hetch
el.hetch - avatar
0
you are amazing guys . it is working ,it is working , yes yes yes . thanks alot
12th Feb 2017, 10:47 AM
el.hetch
el.hetch - avatar