【问题标题】:tkinter Multiple Buttons Colour Changetkinter 多个按钮颜色更改
【发布时间】:2016-11-07 19:24:18
【问题描述】:

我正在使用 tkinter 创建一个 8x8 按钮矩阵,当按下单个按钮时添加到最终列表(例如 finalList = ((0,0),(5,7),(6,6), ...),允许我快速创建 8x8 (x,y) 坐标图像。我已经创建了带有按钮的窗口,但现在尝试在函数中引用这些按钮以添加到列表甚至更改按钮颜色

我已经读过,一旦创建了按钮并创建了另一个按钮,它就会移动到该按钮引用。我怀疑我需要使用 dict 或 2D 数组来存储所有这些按钮引用,但我正在努力寻找解决方案。

from tkinter import *

class App:

    def updateChange(self):
        '''
        -Have the button change colour when pressed
        -add coordinate to final list
        '''
        x , y = self.xY
        self.buttons[x][y].configure(bg="#000000")

    def __init__(self, master):
        frame = Frame(master)
        frame.pack()

        self.buttons = [] # Do I need to create a dict of button's so I can reference the particular button I wish to update?
        for matrixColumn in range(8):
            for matrixRow in range(8):
                self.xY = (matrixColumn,matrixRow)
                stringXY = str(self.xY)
                self.button = Button(frame,text=stringXY, fg="#000000", bg="#ffffff", command = self.updateChange).grid(row=matrixRow,column=matrixColumn)
                self.buttons[matrixColumn][matrixRow].append(self.button)


root = Tk()
app = App(root)
root.mainloop()

Example of the 8x8 Matrix

【问题讨论】:

    标签: python python-3.x button tkinter command


    【解决方案1】:

    self.xY 在你的双 for 循环中设置为 7,7 并且永远不会改变。如果您希望每个按钮的设置不同,您可能需要更改 updateChange 以采用两个参数 (x,y),并将它们作为按钮的命令传递,使用类似的东西; lambda x=matrixColumn y=matrixRow: self.updateChange(x,y)

    例如updateChange

    def updateChange(self, x, y):
        '''...'''
        self.buttons[x][y].configure(bg="black")
    

    【讨论】:

    • 感谢您的帮助,您完全正确,我的 self.xY 最终成为 (7,7)。尝试这个时没想到聪明!
    【解决方案2】:

    以下是 2 个示例,第一个是如果您只想更改颜色而没有其他内容,那么您可以不使用列表来完成。第二个涉及使用列表并演示 Delioth 指出的内容

    class App(object):
        def __init__(self, master):
            self._master = master
    
            for col in range(8):
                for row in range(8):
                    btn = tk.Button(master, text = '(%d, %d)' % (col, row), bg = 'white')
                    btn['command'] = lambda b = btn: b.config(bg = 'black')
                    btn.grid(row = row, column = col)
    
    class App(object):
        def __init__(self, master):
            self._master = master
            self._btn_matrix = []
    
            for col in range(8):
                row_matrix = []
                for row in range(8):
                    btn = tk.Button(master, text = '(%d, %d)' % (col, row), bg = 'white',
                                    command = lambda x = row, y = col: self.update(x, y))
                    btn.grid(row = row, column = col)
                    row_matrix.append(btn)
                self._btn_matrix.append(row_matrix)
    
        def update(self, row, col):
            self._btn_matrix[col][row].config( bg = 'black' )
    
    if __name__ == '__main__':
        root = tk.Tk()
        app = App(root)
        root.mainloop()
    

    【讨论】:

    • 感谢您的帮助,使用了您的第二个示例,它很有魅力。非常感谢您的帮助,希望我能自己管理其余的想法。一定会从中吸取教训!
    猜你喜欢
    • 2014-02-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-04
    • 2017-12-19
    • 2016-05-26
    • 2020-10-09
    • 1970-01-01
    相关资源
    最近更新 更多