简体   繁体   English

tkinter制作7x7按钮并在单击时更改颜色

[英]tkinter make 7x7 buttons and change color when clicked

I'm writing a program with a GUI for school. 我正在用GUI编写用于学校的程序。

I'm supposed to write Pah Tum. 我应该写帕姆·图姆。 Its played on a 7x7 matrix. 它以7x7矩阵播放。 My console game is done and works perfectly, but I'm struggling with Tkinter right now. 我的主机游戏已经完成并且可以完美运行,但是我现在正与Tkinter苦苦挣扎。

So far I have a few frames inside frames defined in my __init__ function. 到目前为止,我在__init__函数中定义的框架内有一些框架。

So now the first problem: 现在是第一个问题:

I could make a 7x7 board like this: 我可以像这样制作一个7x7的电路板:

for x in range(0, 7):
    for y in range(0, 7):
            self.button = Button(self.board, command=self.action)
            self.button.grid(row = x, column = y)

So what I want to do now is, every time I hit a button I want to change the color to red (for player 1) or blue (if its player 2s turn). 所以我现在想做的是,每当我按下一个按钮时,我想将颜色更改为红色(对于播放器1)或蓝色(如果其播放器2s转动)。

    self.turn_tracker = 0  # is in __init__

def action(self):
    if self.turn_tracker in range(0, 49, 2):
        self.turn_tracker += 1
        self.button.configure(bg="blue")

    elif self.turn_tracker in range(1, 49, 2):
        self.turn_tracker += 1
        self.button.configure(bg="red")

    elif self.turn_tracker == 49:
        print("game over")
        sys.exit() #calculate

This will only change the button at 6x6. 这只会将按钮更改为6x6。 So what I tried is defining every button separately and having a change method for every button separately as well. 因此,我尝试的是分别定义每个按钮,并分别为每个按钮设置一个更改方法。 As you can imagine this looks really really ugly but it works at least. 可以想象,这看起来确实很丑陋,但至少有效。

What can I do to make it more efficient? 我该怎么做才能使其更有效率? Thanks for any help! 谢谢你的帮助!

You can still define your buttons in a loop and associate to them different commands. 您仍然可以在循环中定义按钮并将它们与不同的命令关联。 For instance you can store the buttons in a 7x7 matrix self.buttons and make action take the row and column as arguments: 例如,您可以保存按钮,输入的7x7矩阵self.buttons ,使action采取的行和列作为参数:

def action(self, x, y):
    if self.turn_tracker in range(0, 49, 2):
        self.turn_tracker += 1
        self.buttons[x][y].configure(bg="blue")

    elif self.turn_tracker in range(1, 49, 2):
        self.turn_tracker += 1
        self.buttons[x][y].configure(bg="red")

    elif self.turn_tracker == 49:
        print("game over")

And the matrix of buttons is created in __init__ with 然后在__init__使用创建按钮矩阵

    self.buttons = []
    for x in range(0, 7):
        self.buttons.append([])
        for y in range(0, 7):
            button = tk.Button(self.board, command=lambda row=x, column=y: self.action(row, column))  
            self.buttons[x].append(button)
            button.grid(row=x, column=y)

I use lambda row=x, column=y: self.action(row, column) and not directly lambda: self.action(x, y) because at the end of the loop x=6 and y=6 , so when I would click on a button, it will alwys be the color of the last button that would change (see Tkinter assign button command in loop with lambda ). 我使用lambda row=x, column=y: self.action(row, column)而不是直接使用lambda: self.action(x, y)因为在循环结束时x=6y=6 ,所以当我将单击一个按钮,它将始终是将更改的最后一个按钮的颜色(请参阅lambda循环中的Tkinter Assign button命令 )。

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

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