Home > Software design >  how to remove previous input and only show current input in key binding of python
how to remove previous input and only show current input in key binding of python

Time:06-16

say if you input 123 then 456, the program will gives you the following result: "Weight is: 123g" "Weight is: 456g"

how to code so that it only gives me the current result with previous result being removed. "Weight is: 456g"


from tkinter import *
  
# Create a GUI app
app = Tk()
  
# Set the title and geometry of the app
app.title('Bind Number Keys')
app.geometry("800x400")
reading = ''  


# Make a function to display a message
# whenever user presses 0-9 key
def key_press(self):
    global reading
    if (self.keysym == 'Return' or self.keysym == 'KP_Enter'):
        if reading != '':
            Label(app, text="Weight is: "   reading   'g', font='Arial 16 bold').pack()
            reading = ''
    else:
        reading  = self.char    
    
# Create a label widget to display the text
label = Label(app, text="Key in your weight")
label.pack(pady=25)
label.config(font='Arial 20 bold')
  
# Bind all the number keys with the callback function
#for i in range(10):
#   app.bind(str(i), key_press)
app.bind_all('<Key>', key_press)

# Make infinite loop for displaying app on the screen
app.mainloop()

CodePudding user response:

Instead of creating a new label on each user entry create a label only once and update the text= property of the label to the current value with .config(). You don't need to remove/destroy the label and create a new one each time you need to show a new text in the label or change other label properties. See the code below where the label is created only once and updates its text according to user input:

# https://stackoverflow.com/questions/72614933/how-to-remove-previous-input-and-only-show-current-input-in-key-binding-of-pytho
from tkinter import *
  
# Create a GUI app
app = Tk()
  
# Set the title and geometry of the app
app.title('Bind Number Keys')
app.geometry("800x400")
reading = ''  
objLabel = None

# Make a function to display a message
# whenever user presses 0-9 key
def key_press(self):
    global reading, objLabel
    if (self.keysym == 'Return' or self.keysym == 'KP_Enter'):
        if reading != '':
            if objLabel is None:
                objLabel = Label(app, text="Weight is: "   reading   'g', font='Arial 16 bold')
                objLabel.pack()
            else: 
                objLabel.config(text="Weight is: "   reading   'g')
        reading = ''
    else:
        reading  = self.char    
    
# Create a label widget to display the text
label = Label(app, text="Key in your weight")
label.pack(pady=25)
label.config(font='Arial 20 bold')
  
# Bind all the number keys with the callback function
#for i in range(10):
#   app.bind(str(i), key_press)
app.bind_all('<Key>', key_press)

# Make infinite loop for displaying app on the screen
app.mainloop()

CodePudding user response:

It will work

from tkinter import *
  
# Create a GUI app
app = Tk()
  
# Set the title and geometry of the app
app.title('Bind Number Keys')
app.geometry("800x400")
reading = ''  


# Make a function to display a message
# whenever user presses 0-9 key
label = Label(app, text="Key in your weight")
label.pack(pady=25)

my_label = Label(app, font='Arial 16 bold')
my_label.pack()
def key_press(self):
    global reading
    if (self.keysym == 'Return' or self.keysym == 'KP_Enter'):
        if reading != '':
            t = "Weight is: "   reading   'g'
            my_label.config(text=t)
            app.update()
            reading = ''
    else:
        reading  = self.char    
# Create a label widget to display the text

label.config(font='Arial 20 bold')
  
# Bind all the number keys with the callback function
#for i in range(10):
#   app.bind(str(i), key_press)
app.bind_all('<Key>', key_press)

# Make infinite loop for displaying app on the screen
app.mainloop()
  • Related