2

i'm completely new to python and try to wrap my head about the following problem. I need to create buttons from an sql query. Right now one button looks like this (i skip style things)

def __init__ (self, screen):

    self.frame = Frame(screen)
    self.frame.grid()            
    self.button01Image = PhotoImage(file=row[2])
    self.button01 = Button(
        self.frame,
        image = self.button01Image,
        command = self.button01Activate,
        )
    self.button01.grid(row = 0, column = 0)


    def button01Activate(self):
        self.button01.configure(
            state = DISABLED
            )
        dosomething(23)
        dosomething(01)

result (row) from sql query looks like this

('01', '23', 'picturefilename.png' ,'0')

In the end i want something like this, yes i know that it doesn't work.

def __init__ (self, screen):

    self.frame = Frame(screen)
    self.frame.grid()            
    self.button + row[0] Image = PhotoImage(file=row[2])
    self.button + row[0]  = Button(
        self.frame,
        image = self.button01Image,
        command = self.button01Activate,
        )
    if row[3] == 0
        self.button + row[0] + .configure(
            state = DISABLED
            )
    #counting up to a 3x5 grid)
    self.button + row[0] + .grid(row = 0, column = 0)


    def button + row[0] + Activate(self):
        self.button + row[0] + .configure(
            state = DISABLED
            )
        dosomething(row[1])
        dosomethingelse(row[0])

So basically i need to assign functions and pictures to the button from the database.

I got no clue where to start change things here, i guess my approach is completely wrong. Can somebody point me into the right direction?

1 Answer 1

1

You have to construct the Activate functions through a closure.

I have replaced the images with text for the example. Just look at the comments.

#!/usr/bin/env python3
import tkinter as tk

class CreateButtons:
    def __init__ (self, screen, sqlresult):

        self.frame = tk.Frame(screen)
        self.frame.grid()
        self.buttons = {}
        for i,row in enumerate(sqlresult):
            #buttonImage = PhotoImage(file=row[2])
            buttonId = row[0]
            button = tk.Button(
              self.frame,
              text = 'button-'+str(buttonId)
              #image = buttonImage
            )
            button['command'] = self.createButtonCommand(buttonId, row[1])
            self.buttons[buttonId] = button
            if row[3] == 0:
                button.configure(state = tk.DISABLED)
            #counting up to a 3x5 grid)
            button.grid(row = i//5, column = i%5)

    def createButtonCommand(self, buttonId, buttonArg):
        def command():
            self.buttonActivate(buttonId, buttonArg)
        return command

    def buttonActivate(self, buttonId, buttonArg):
        button = self.buttons.get(buttonId)
        if not button: return
        button.configure(state = tk.DISABLED)
        print("Button Pressed:", buttonId)
        #dosomething(buttonArg)
        #dosomethingelse(buttonId)


sqlresult = [(0,1,'pict1.png',1),(4,6,'pict2.png',0),(10,11,'pict3.png',1)]
root = tk.Tk()
CreateButtons(root, sqlresult)
root.mainloop()
Sign up to request clarification or add additional context in comments.

2 Comments

That does exactly what i was looking for, thank you!
@OkayDokey: Maybe you can mark it as selected Answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.