单击该按钮时更改按钮颜色

时间:2016-03-31 19:00:57

标签: python tkinter

我正在制作迷宫,用户输入尺寸,然后点击按钮将该按钮的颜色更改为黑色。我最终想要的是制作一个ai,它将尝试导航用户使用黑色矩形创建的迷宫,ai不允许继续使用。 问题是我不知道如何更改单击按钮的属性,因为用于创建的嵌套循环它们都具有相同的名称。

from tkinter import *
import tkinter as tk
from tkinter.ttk import Combobox,Treeview,Scrollbar

class MainMenu(Frame):
    def __init__(self, master):
        """ Initialize the frame. """
        super(MainMenu, self).__init__(master)
        self.grid()
        self.frame1 = tk.LabelFrame(self, text="entering diemsions", width=300, height=130, bd=5)
        self.frame1.grid(row=0, column=0, columnspan=3, padx=8)
        self.frame2 = tk.LabelFrame(self, text="creating maze", width=300, height=130, bd=5)
        self.frame2.grid(row=1, column=0, columnspan=3, padx=8)
        self.create_GUI()

    def create_GUI(self):
        self.width_lbl = Label(self.frame1, text = "width:")
        self.width_lbl.grid(row = 1 , column = 1)
        self.width_txt = Entry(self.frame1)
        self.width_txt.grid(row = 1, column = 2)

        self.getdimensions_btn = Button(self.frame1, text = "enter dimensions",command = lambda:self.createmaze())
        self.getdimensions_btn.grid(row = 1 , column = 3)

        self.height_lbl = Label(self.frame1, text = "height:")
        self.height_lbl.grid(row = 1 , column = 4)
        self.height_txt = Entry(self.frame1)
        self.height_txt.grid(row = 1, column = 5)

    def createmaze(self):
        width = int(self.width_txt.get())
        height = int(self.height_txt.get())
        for widthcount in range (width):
            for heightcount in range(height):
                self.maze_btn = Button(self.frame2, text = "",width = 4, height = 2)
                self.maze_btn.grid(row = heightcount , column = widthcount)
                self.maze_btn.bind("<Button-1>", self.disablebtn)

    def disablebtn(self,event):
        grid_info = event.widget.grid_info()
        col = grid_info["column"]
        col = int(col)
        row = grid_info["row"]
        row = int(row)


root = Tk()
root.title("hi")
root.geometry("500x500")
root.configure(bg="white")
app = MainMenu(root)
root.mainloop()

0 个答案:

没有答案