简体   繁体   English

如何在tkinter python中更改最后单击的按钮的文本

[英]How to change the text of the last clicked button in tkinter python

I have dynamically created a buttons by looping through a list: 我通过遍历列表来动态创建了一个按钮:

AuxName = Button(GUI_1, text=List_Aux[x], font=("Arial", 10))
tk_rgb = "#%02x%02x%02x" % (0,0,0)
AuxName["fg"] = tk_rgb
tk_rgb = "#%02x%02x%02x" % (255,255,255)
AuxName["bg"] = tk_rgb
AuxName.place(x=BUTTON_LEFT_AUX1, y=BUTTON_TOP_AUX1 , height=BUTTON_HEIGTH_AUX1, width=BUTTON_WIDTH_AUX1)
AuxName["command"] = ButtonClick

Each button has a different AuxName value (1btn1, 1btn2, 1btn3, etc) numbers being the column and the row where each button is located. 每个按钮都有不同的AuxName值(1btn1、1btn2、1btn3等),它们是每个按钮所在的列和行。

The problem I'm having is that now I want the function ButtonClick to get the name of the button being clicked. 我遇到的问题是,现在我想要功能ButtonClick来获取被单击按钮的名称。 The last thing I tried was: 我尝试的最后一件事是:

def ButtonClick():
1btn1["text"] = "Goodbye"

But python throws a syntax error (and points just before the opening bracket). 但是python引发语法错误(并指向开括号之前)。

How could I get the last clicked button name, and then change its text through the function ButtonClick() ? 如何获得最后单击的按钮名称,然后通过功能ButtonClick()更改其文本?

-------------------- Edit to the first answer, almost working perfectly now ------------ --------------------编辑第一个答案,现在几乎可以正常工作了------------

Thank you for the answer, I appreciate it very much, and makes it work almost perfect. 谢谢您的回答,我非常感谢它,并使它的运行效果几乎完美。 There are two flaws, first of them is it will throw an error because of the command ButtonClick being sent with no argument, which is the default way to send commands of the button binds, so I dont really know how to fix that... And the second is, after using the code you provided me, buttons text will change as you especified, but they wont respect the position and size previously defined by me. 有两个缺陷,首先是由于不带参数发送命令ButtonClick而将引发错误,这是发送按钮绑定命令的默认方式,所以我真的不知道该如何解决...第二个是,在使用了您提供的代码后,按钮文本将按照您的指定进行更改,但是它们将不遵守我之前定义的位置和大小。

BUTTON_TOP_AUX1 = 20
BUTTON_WIDTH_AUX1 = 400
BUTTON_LEFT_AUX1 = 5
BUTTON_HEIGTH_AUX1 = 20
Buttons_Function = "DesiredFunctionOne"
BUTTON_COLUMN_AUX1 = 1
List_Size_Aux = len(List_Aux)
for x in range ( List_Size_Aux ):
    AuxName = str(BUTTON_COLUMN_AUX1)+'btn'+str(x)
    AuxName = Button(GUI_1, text=List_Aux[x], font=("Arial", 10))
    tk_rgb = "#%02x%02x%02x" % (0,0,0)
    AuxName["fg"] = tk_rgb
    tk_rgb = "#%02x%02x%02x" % (255,255,255)
    AuxName["bg"] = tk_rgb
    AuxName.place(x=BUTTON_LEFT_AUX1, y=BUTTON_TOP_AUX1 , height=BUTTON_HEIGTH_AUX1, width=BUTTON_WIDTH_AUX1)
    AuxName["command"] = ButtonClick
    BUTTON_TOP_AUX1 = BUTTON_HEIGTH_AUX1+BUTTON_TOP_AUX1
    AuxName.bind("<Button-1>", ButtonClick)
    AuxName.pack()

Could you please tell me how to fix those? 您能告诉我如何解决这些问题吗? Greatly appreciated :) 不胜感激 :)

First you have to bind the function to an event, instead of using the command attribute. 首先,您必须将函数绑定到事件,而不是使用command属性。 For a left click on a widget, remove the line 在窗口小部件上单击鼠标左键,删除该行

And use the event <Button-1> instead: 并改用事件<Button-1>

AuxName.bind("<Button-1>", ButtonClick)

Then in the event handler you receive an event argument. 然后,在事件处理程序中,您将收到一个事件参数。 It has a widget field, so you can get the widget which triggered the event: 它具有一个widget字段,因此您可以获得触发事件的小部件:

def ButtonClick(event):
    event.widget['text'] = 'Goodbye'

Remembar that you have to remove the AuxName['command'] = ButtonClick , because if you don't do so, ButtonClick will be executed two times (one of them without any argument). 请记住,您必须删除AuxName['command'] = ButtonClick ,因为如果不这样做, ButtonClick将被执行两次(其中一次不带任何参数)。

When you change the text of the Button, it will modify also its width since the new text may have a different lenght. 当您更改按钮的文本时,由于新文本的长度可能会有所不同,因此它也会更改其宽度。 One problem you have in respect to the position of the widgets is that you are using both place and pack as geometry managers: 关于小部件的位置,您遇到的一个问题是您同时使用了placepack作为几何管理器:

AuxName.place(...)
# ...
AuxName.pack()

In Tkinter, you should use consistently one geometry manager. 在Tkinter中,应始终使用一个几何图形管理器。 In this case, I suggest you to use the grid geometry manager , which is the easiest to use: 在这种情况下,建议您使用网格几何管理器 ,这是最容易使用的:

AuxName.grid(row=x, column=y, ...)

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

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