简体   繁体   English

每次按下按钮时更改标签的文本

[英]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. 我正在TKINTER中使用FOR做一个PYTHON表,我想每次在ENTRY中放置一个新数字时,标签都会更改为新表。 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. 例如,一个数字将被放置在ENTRY中,此人将单击TAB按钮,单击该按钮时,将出现该表格,但是如果该人想要另一个数字,然后再次单击,则新表将从前一个。 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. 我想知道如何单击tabuada按钮,而上一个按钮不使用另一个按钮就擦除了新按钮。在下面获取代码和照片,谢谢。 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. 您的代码中未定义c ,这引发了异常。
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. 我将标签的构造放置在功能make_label ,该功能从main块调用,并从click()调用,以在请求新数字时重建它。 reset was missing a call to pack on the label, to update the text displayed. reset丢失了pack在标签上以更新显示的文本的调用。

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. 我修改了reset以便销毁了标签并从视图中重新创建了标签,从而消除了窗口尺寸的增加。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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