Python Forum
display confusion
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
display confusion
#1
I simply cannot get the result displayed from the code below. Where do I go wrong?


from  tkinter import*

def kalkuliere():
    result = int(number.get())*2

main_window = Tk()
main_window.title("Calculator")
main_window.geometry("300x200")
number = StringVar()
number.set("0")
result = StringVar()
result_label = Label(main_window, text = "Result").grid(column = 1, row = 1)
result_value = Label(main_window, textvariable = result).grid(column = 2, row = 1)
number_label = Label(main_window, text = "Number to calculate").grid(column = 1, row = 2)
number_entry =Entry(main_window, width = 10,textvariable = number).grid(column = 2, row = 2)

calc_button = Button(main_window, text = "Calculate", command = kalkuliere).grid(column = 2, row = 3)

main_window.mainloop()
Reply
#2
Forget it. I saw now
Reply
#3
You have not set the tk StringVar result ie result.set(int(number.get()) * 2), the following will make the result update.
from  tkinter import*
 
main_window = Tk()
main_window.title("Calculator")
main_window.geometry("300x200")
number = StringVar()
number.set("0")
result = StringVar()
result_label = Label(main_window, text="Result").grid(column=1, row=1)
result_value = Label(main_window, textvariable=result).grid(column=2, row=1)
number_label = Label(main_window, text="Number to calculate").grid(column=1, row=2)
number_entry = Entry(main_window, width=10, textvariable=number).grid(column=2, row=2)

 
def kalkuliere():
    result.set(int(number.get()) * 2)


calc_button = Button(main_window, text="Calculate", command=kalkuliere).grid(column=2, row=3)
 
main_window.mainloop()
It would be advisable to start using classes when dealing with GUI code.
Also see https://python-forum.io/Thread-Namespace...th-imports
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Display and update the label text which display the serial value jenkins43 5 4,299 Feb-04-2019, 04:36 AM
Last Post: Larz60+
  Display more than one button in GUI to display MPU6000 Sensor readings barry76 4 1,371 Jan-05-2019, 01:48 PM
Last Post: wuf

Forum Jump:

User Panel Messages

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