Home > Back-end >  How to get a widget value created in a for loop when button is clicked in python tkinter
How to get a widget value created in a for loop when button is clicked in python tkinter

Time:12-17

I'm trying to create a basic counting app so that I can go in the field and count multiple items at once. I created the majority of the widgets with a for loop and I am trying to get the value of the label self.count and add to it.

Is there a way to retrieve the values with the references I have stored?

Since the values are in different memory locations and I don't know how to tell which button is pressed based on the below code.

(Note: the buttons should only interact with the values directly above them.)

import tkinter as tk
from tkinter import ttk
from dataclasses import dataclass

@dataclass
class fwidgets:
    frame: str
    framenum: int
    widnum: int
    widgets: list
    ref: list

class App:    
    def addcount(self):
        pass
    
    def resetcount(self):
        pass
    
    def main(self):
        win = tk.Tk()
        largedoublefrm = ttk.Frame(win)
        largesinglefrm = ttk.Frame(win)
        smalldoublefrm = ttk.Frame(win)
        smallsinglefrm = ttk.Frame(win)
        combofrm = ttk.Frame(win)

        largedoublefrm.grid(column=2, row=0)
        largesinglefrm.grid(column=0, row=0)
        smalldoublefrm.grid(column=3, row=0)
        smallsinglefrm.grid(column=1, row=0)
        combofrm.grid(column=4, row=0)

        mainframe = fwidgets('win', 200, 5, ('largesinglefrm', 'smallsinglefrm',
                            'largedoublefrm', 'smalldoublefrm', 'combofrm'),
                            (largesinglefrm, smallsinglefrm, largedoublefrm,
                            smalldoublefrm, combofrm))

        self.refframe = []
        x=0
        for wid in mainframe.ref:
            ttk.Label(wid, text=mainframe.widgets[0]).pack()
            self.count = ttk.Label(wid, text='0', font=("Arial", 20))
            add = tk.Button(wid, text='ADD', height=5, width=15,
                            command=self.addcount)
            reset = tk.Button(wid, text='RESET', height=5, width=15,
                              command=self.resetcount)
            self.count.pack()
            add.pack()
            reset.pack()
            frame = fwidgets(mainframe.widgets[x], x, 3, ('count', 'add', 'reset'),
                             (self.count, add, reset))
            self.refframe.append(frame)
            x  = 1
        print(self.refframe)
        win.mainloop()
    
if __name__ == '__main__':
    app = App()
    app.main()

I'm not sure where to go from here. Any help would be appreciated.

CodePudding user response:

To find the relevant objects first remove win.mainloop(), since only one mainloop is permitted per tk.tk instance.

Then modify your code like this.

def addcount(self, i):
    print(i.winfo_children())


def resetcount(self, i):
    print(i.winfo_children())


add = tk.Button(wid, text='ADD', height=5, width=15,
                command = lambda w = wid: self.addcount(w))
reset = tk.Button(wid, text='RESET', height=5, width=15,
                  command = lambda w = wid: self.resetcount(w))

# win.mainloop()

Now when you click a button a list of relevant objects will be displayed.

  • Related