I'm just starting with python and i'm having a problem. I've tried various solutions, but i can't update the field that says '19'. When i click on plus, i want it to be 20, then 21,... and when i click - it has to go back to 20 , 19. Can anybody tell me how to solve this?
from tkinter import *def fct_tempplus():while True:# tekstvak_input_user = tekstvak_input_user +1return tekstvak_input_user + 1def fct_tempmin():print ('ok')window = Tk()window.geometry("800x400") # not *window.title("TEST")label= Label( window, text = "Temp?")label.place(x=350,y=175)tempplus=Button(window, bd=10,width=10, height = 1,text="+",command=fct_tempplus,
font=("Helvetica", 12))tempplus.place(x=500,y=150)tempmin=Button(window, bd=10,width=10, height = 1,text="-", font=("Helvetica", 12),command=fct_tempmin)tempmin.place(x=500,y=200)tekstvak_input_user = Entry(window, width = 10 )tekstvak_input_user.insert(0,19.0)tekstvak_input_user.place(x=350 , y=200)window.mainloop()`
while True
is not needed in this program. And you have to use .get()
to get a value inside a function. And then you should store it in a globalized variable, convert it into int
or float
. Then, simply use delete(0, END)
to clear what's inside the Entry
widget and then use insert()
to insert the new value in the Entry
.
Like This:
from tkinter import *var = 0def fct_temp_plus():global varvar = float(tekstvak_input_user.get())var += 1tekstvak_input_user.delete(0, END)tekstvak_input_user.insert(0, var)def fct_temp_min():global varvar = float(tekstvak_input_user.get())var -= 1tekstvak_input_user.delete(0, END)tekstvak_input_user.insert(0, var)window = Tk()
window.geometry("800x400") # not *
window.title("TEST")label = Label(window, text="Temp?")
label.place(x=350, y=175)temp_plus = Button(window, bd=10, width=10, height=1, text="+", command=fct_temp_plus, font=("Helvetica", 12))
temp_plus.place(x=500, y=150)temp_min = Button(window, bd=10, width=10, height=1, text="-", font=("Helvetica", 12), command=fct_temp_min)
temp_min.place(x=500, y=200)tekstvak_input_user = Entry(window, width=10)
tekstvak_input_user.insert(0, 19.0)
tekstvak_input_user.place(x=350, y=200)window.mainloop()
Note: You should always import tkinter as tk
.
Like This:
import tkinter as tkvar = 0def fct_temp_plus():global varvar = float(tekstvak_input_user.get())var += 1tekstvak_input_user.delete(0, tk.END)tekstvak_input_user.insert(0, var)def fct_temp_min():global varvar = float(tekstvak_input_user.get())var -= 1tekstvak_input_user.delete(0, tk.END)tekstvak_input_user.insert(0, var)window = tk.Tk()
window.geometry("800x400")
window.title("TEST")label = tk.Label(window, text="Temp?")
label.place(x=350, y=175)temp_plus = tk.Button(window, bd=10, width=10, height=1, text="+", command=fct_temp_plus, font=("Helvetica", 12))
temp_plus.place(x=500, y=150)temp_min = tk.Button(window, bd=10, width=10, height=1, text="-", font=("Helvetica", 12), command=fct_temp_min)
temp_min.place(x=500, y=200)tekstvak_input_user = tk.Entry(window, width=10)
tekstvak_input_user.insert(0, 19.0)
tekstvak_input_user.place(x=350, y=200)window.mainloop()