简体   繁体   中英

Change the label's text everytime a button is pressed

I'm doing a PYTHON table using FOR, in TKINTER, and I would like every time a new number is placed in ENTRY, the label changes to the new table. For example, a number will be placed in the ENTRY and the person will click the TAB button, when it is clicked, the table will appear, but if the person wants another number, and click again, the new table will go down from the previous one. My solution was to create a button that erases the previous table, but when the button is pressed, only the last multiplication is deleted. I would like to know how I click the tabuada button, and the previous one erases the new one without using another button .Get the code and a photo below, Thanks. Obs.: First photo shows reset button working, but it just erase the last multiplication,second photo shows the whole multiplication.

from tkinter import *
import tkinter as tk

win=tk.Tk()
win.title('Table')
lb=Label(win,text='Type a number:',font='Helvetica 12 bold')
lb.pack()
e=Entry(win)
e.pack()

def click():
   global c
   c=e.get()
   print('requested number ',c)

for b in (range(0, 11)):
   global lb2
   lb2=Label(text='{} x {} = {} '.format(c, b, int(b)*int(c)))
   lb2.pack()

def reset():
   lb2['text'] = ' '

bt1=Button(win,text='GO',bg='lightblue',command=click)
bt1.pack()
bt2=Button(win,text='RESET',bg='lightblue',command=reset)   
bt2.pack()
win.mainloop()

erasing:

抹去

whole multiplication:

整体乘法

Here are some fixes for your code; it was not entirely clear what you exactly meant by: "the new table will go down from the previous one." , so I went with having the new table replacing the previous one.

c was not defined in your code and that threw an exception.
I placed the construction of the label inside a function make_label that is called from the main block, and from click() , to rebuild it when a new number is requested. reset was missing a call to pack on the label, to update the text displayed.

I think that should help you get started in the right direction; let me know if something is unclear.

edit:

I modified reset so the label is destroyed and re-created from view, thus removing the growth in size of the window.

from tkinter import *
import tkinter as tk

win=tk.Tk()
win.title('Table')
lb=Label(win,text='Type a number:',font='Helvetica 12 bold')
lb.grid(row=0, column=0)
lb2 = Label(text='')
e=Entry(win)
e.grid(row=1, column=0)
c = 2

def click():
    global c
    c = e.get()
    print('requested number ', c)
    reset()
    make_label(c)

def make_label(c):
    global lb2
    txt = []
    for b in (range(0, 11)):
        txt.append('{} x {} = {} '.format(c, b, int(b)*int(c)))
    text = '\n'.join(txt)
    lb2 = Label(text=text)
    lb2.grid(row=4, column=0)

def reset():
    global lb2
    lb2.destroy()
    lb2 = Label()
    lb2.grid(row=4, column=0)

make_label(c)

bt1=Button(win,text='GO',bg='lightblue',command=click)
bt1.grid(row=2, column=0)
bt2=Button(win,text='RESET',bg='lightblue',command=reset)   
bt2.grid(row=3, column=0)
win.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM