Python update user input with tkinter button

2024/11/17 4:55:50

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()`
Answer

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()
https://en.xdnf.cn/q/120243.html

Related Q&A

Python Unique DF in loop [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed 5…

TypeError: unsupported operand type(s) for +=: NoneType and str

I am new to Python and Im sure that Im doing something wrong - I would like to ask a user for three numbers and print their sum, heres my current code:for i in range(0, 3):total = Nonenum = input(Pleas…

multiply list of ndarrays by list

I want to multiply a list of numbers, by a ndarray, that is, each number in the list multiply by the first ndarray. This is the list: list1 = [840,845,897]This is the list of ndarray list = df[Example]…

Python skipping last element in while iterating a list using for loop [duplicate]

This question already has answers here:Strange result when removing item from a list while iterating over it in Python(12 answers)Closed 9 years ago.class Sample:def __init__(self):self.lst_report_foot…

can this code be shortened or improved? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.This question does not appear to be about programming within the scope defined in the help center.Cl…

Count Running and Stopped Ec2 Instances with AWS Lambda

How can I count number of running and stopped EC2 instances in a particular region using boto3 and an AWS Lambda function?

Python2.7: How to split a column into multiple column based on special strings like this?

Im a newbie for programming and python, so I would appreciate your advice!I have a dataframe like this.In info column, there are 7 different categories: activities, locations, groups, skills, sights, t…

python - return and print does not give same result

what Im trying to do is take the entered string separated by commas and change it to list then for each list as a key print the associated value.def main():myDict = {a:1, b:2, c:3, d:4, e:5....}u_input…

Creating a menuBar in a frame?

import Tkinter as tk from Tkinter import Label, StringVar, Entry, Button, Menu, Frame, Tk, Canvas import subprocess from Tkconstants import LEFT, CENTER,W, SUNKEN , X, Yclass SampleApp(tk.Tk):def __ini…

Python return dictionary [closed]

Closed. This question needs debugging details. It is not currently accepting answers.Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to repro…