【问题标题】:change button text, using button name使用按钮名称更改按钮文本
【发布时间】:2021-04-01 09:46:20
【问题描述】:

我有一个问题,我使用循环创建了一个按钮表,并将按钮的名称保存在一个列表中,现在我想在单击一个按钮时更改按钮的文本。 我不知道怎么做。

这是我创建表的循环

     def grid(n):
                i = n*n
                for widget in LeftFrame.winfo_children():
                    widget.destroy()
                for i in range(n):
                    for row in range(n):
                        for col in range(n):
                            button = Button(LeftFrame, text=' ', width=12, height=6, command=lambda: checker(button, i))
                            button.grid(row=row, column=col)
                            button_identities.append(button)

这是点击按钮的功能

        def checker(buttons, i):
            print(i)
            global click, button_identities
            print(button_identities)
            bname = (button_identities[i])
            print(bname)
            if buttons["text"] == ' ' and click == True:
                buttons["text"] = 'X'
                click = False
                # scorekeeper()
            elif buttons['text'] == ' ' and click == False:
                buttons['text'] = 'O'
                click = True
                # scorekeeper()

有一种方法可以从最后一个函数中的按钮名称中检查文本

【问题讨论】:

  • 你现在不能,你必须创建一个类来存储这些按钮,因为它们都必须是独立的,或者至少它会更容易
  • 对不起,不知道如何让它们独立于一个类,我该怎么做?

标签: python tkinter button


【解决方案1】:

试试这个:

from functools import partial

def grid(n):
    for widget in LeftFrame.winfo_children():
        widget.destroy()
    for i in range(n):
        for row in range(n):
            for col in range(n):
                button = Button(LeftFrame, text=' ', width=12, height=6)
                # Assign the button a command
                command = partial(checker, button)
                button.config(command=command)

                button.grid(row=row, column=col)
                button_identities.append(button)

def checker(button):
    # The argument is a button
    global click

    # If the cell is empty
    if button.cget("text") == " ":
        # If Xs are you play
        if click:
            # Change the text of the button
            button.config(text="X")
            click = False
        # If Os are to play
        else:
            # Change the text of the button
            button.config(text="O")
            click = True

我通常使用<widget>.cget("<parameter>") 从小部件中获取参数,并使用<widget>.config(parameter=<new value>) 为其分配新值。您也可以使用functools.partial 为按钮命令传递参数。

【讨论】:

    【解决方案2】:

    这是我对这个问题的看法:

    from tkinter import Tk, Button
    
    
    root = Tk()
    
    
    class EditableButton(Button):
        def __init__(self, parent, row, col):
            Button.__init__(self, parent)
            self.parent = parent
            self.row = row
            self.col = col
            self.tracker = 0
            self.name_list = ['apple', 'pear']
            print(len(self.name_list))
    
            self.button = Button(self.parent, text=self.name_list[self.tracker], command=self.change_text)
            self.button.grid(row=self.row, column=self.col)
    
        def change_text(self):
            if self.tracker < len(self.name_list) - 1:
                self.tracker += 1
            else:
                self.tracker = 0
            self.button.config(text=self.name_list[self.tracker])
    
    
    n = 3
    for row in range(n):
        for col in range(n):
            button = EditableButton(root, row, col)
    
    root.mainloop()
    

    基本上发生的情况是您创建了一个类似于按钮模板的类,并且循环创建的每个类对象都是独立的,每个类对象在内存中都有自己的位置,因此每当您按下按钮时,它只会调用该函数在自己的类中

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-12-09
      • 2011-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多