简体   繁体   English

Python,tkinter:如何在按下时更改按钮的颜色?

[英]Python, tkinter: How to change color of button on press?

Ok, so I have this code that generates a grid of buttons:好的,所以我有这个生成按钮网格的代码:

def click():

    squares[-1][y].configure(bg='blue')

def game(width,height):

    global squares
    squares = []

    global y

    for x in range(width):

        squares.append([0] * height)

        for y in range(height):

            squares[-1][y] = Button(gameWindow,command=click)
            squares[-1][y].grid(column = x, row = (y + 1), sticky =(N+S+E+W))

    for x in range(width):

        Grid.columnconfigure(gameWindow, x, weight = 1)

    for y in range(height):

        Grid.rowconfigure(gameWindow, (y + 1), weight = 1)

    gameWindow.mainloop()

game(8,8)    

I can configure a specific button (1,1) by doing this:我可以通过执行以下操作来配置特定按钮 (1,1):

squares[1][1].configure(bg='blue')

But when I try to configure a button when it is used, it changes the button in the bottom right.但是当我尝试配置一个按钮时,它会更改右下角的按钮。

Any help would be greatly appreciated, thanks in advance.任何帮助将不胜感激,提前致谢。

You have loop你有循环

for y in range(height):

so after this loop y is equal height-1 .所以在这个循环之后y等于height-1

When you click button then it uses squares[-1][y] but y = height-1 so you use always squares[-1][height-1]当你点击按钮时,它使用squares[-1][y]y = height-1所以你总是使用squares[-1][height-1]

You have to assing to button function with argument - button number - using lambda .您必须使用参数 - 按钮编号 - 使用lambda分配按钮功能。
But if you use lambda inside for then you can't do directly lambda:click(y)但是如果你在里面使用lambda for那么你不能直接做lambda:click(y)
but you need lambda arg=y:click(arg)但你需要lambda arg=y:click(arg)

 Button(gameWindow, command=lambda arg=y:click(arg))

And function has to get this argument and use it函数必须得到这个参数并使用它

 def click(arg): 
      squares[-1][arg].config(bg='blue')

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

相关问题 在matplotlib / tkinter中按下按钮后如何更改绘图颜色? - How to change the plot color after a button press in matplotlib/tkinter? 如何在 python 中使用 Tkinter 使用 Python 中的 Tkinter,使用一些选定颜色的迭代来更改 python 中的圆圈颜色,每次按下按钮都会改变颜色? - How to change a circles color in python, using iteration of a few selected colors, which changes with each button press, using Tkinter in python? 如何更改python中Tkinter按钮的on_press“动画” - How to change on_press "animation" of Tkinter button in python 如何在按下按钮时更改Tkinter标签文本 - How to change Tkinter label text on button press 如何在按下 tkinter 后更改按钮 state - How to change button state upon press tkinter 按下按钮时如何更改按钮颜色? - How to change the button color when the button is press? 如何在Python的Tkinter中更改按钮的颜色 - How do I change the color of a button in Python's Tkinter Python 2.7 Tkinter如何更改按钮文本的文本颜色 - Python 2.7 Tkinter how to change text color of a button's text Python tkinter 用按钮改变背景颜色 - Python tkinter change background color with a button 按下按钮后可以更改菜单栏文本吗? (蟒蛇,tkinter) - Can I change the menubar text after press a button? (Python, tkinter)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM