Python Forum

Full Version: display confusion
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
Forget it. I saw now
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