Thread: tkinter button

    #1
  1. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511

    tkinter button


    alright i need to make a 10x10 grid made up of buttons. i was going to a loop to make the whole thing but i don't know if it would that way. i need it so when you click the button it changes the button color or the text of the button. for example if i had this...

    Code:
    from Tkinter import *
    
    class App:
        def __init__(self, root):
            self.root = root
            chars = "QWERTYUIOP"
            row = 0
            for i in range(10):
                text=chars[i]
                Button(
                        root, text=text, fg="black",
                        command=lambda text=text: self.click(text)
                        ).grid(row=row, column=i*2)
    root = Tk()
    app  = App(root)
    root.mainloop()
    now how could i make it so self.click(text) changes the color of the button i clicked? now if that doesn't work would there be a better way of making 100 buttons rather than just making the one by one?
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2004
    Posts
    394
    Rep Power
    52
    Hi!

    I'm sure there is an easier way, but this one also works:
    Code:
    from Tkinter import *
    
    class App:
        def __init__(self, root):
            self.root = root
            row,col = 0,0
            self.buttons = ["dummy"]
            for index in range(1,101):
                self.buttons.append(
                    Button(root, text=index, fg="black", width=1,
                        command=lambda i=index:self.change(i)))
                self.buttons[-1].grid(row=row, column=col)
                col+=1
                if index%10==0: row+=1; col=0
    
        def change(self,index):
            self.buttons[index].configure(fg="red")
    
                
    root = Tk()
    app = App(root)
    root.mainloop()
    So, I'm creating an array self.buttons and fill it with the Buttons. When a button is clicked, self.change is called, with the position of this Button in the array as an argument. So you can easily configure whatever you want on this button

    Regards, mawe
  4. #3
  5. Caress me down
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2005
    Location
    Pennsylvania
    Posts
    289
    Rep Power
    511
    thanks... thats exactly what i was looking for. the only problem that i am having now is that i disabled the button as well. when i disable the button it negates the color change i put in because it changes the foreground color to a light gray. is there anyway around this? i guess i could do this but is it good programming?

    Code:
        self.x = 0
        def change(self,index):
            if self.x == 1:return
            else:
                self.buttons[index].configure(bg="red", state=DISABLED)
                self.x = 1
    edit- but now that i look at it thats not going to work... no matter what button i click on next, it won't change the color. i could just create of 101 zeros and make it self.x[index] == 1 but that doesn't seem like the best way.

IMN logo majestic logo threadwatch logo seochat tools logo