0

This is a follow-up to that question I posted last time which was on how can I give access of tkinter widgets to modules that I imported. I ended up solving that problem using command=lambda: module_name.function(widget).

main.py

from tkinter import *

import checkbox_operation
import receipt_operation


class cafemanagementsystem:

    def __init__(self, cms):
        self.cms = cms
        cms.title("Cafe Management System")

    self.b1var = IntVar(value=1)
    self.b2var = IntVar(value=1)
    self.b1v = StringVar()
    self.b2v = StringVar()
    self.b1v.set("0")
    self.b2v.set("0")

    self.b1 = Checkbutton(self.bevmenu, command=lambda: checkbox_operation.check(self.b1var, self.b1v, self.b1a, self.b2var, self.b2v, self.b2a), text="Latte", variable=self.b1var, onvalue=1, offvalue=0).grid(
        row=0, column=0, sticky="w")
    self.b1a = Entry(self.bevmenu, bd=2, textvariable=self.b1v)
    self.b1a.grid(row=0, column=1, sticky="w")

    self.b2 = Checkbutton(self.bevmenu, command=lambda: checkbox_operation.check(self.b1var, 
    self.b1v, self.b1a, self.b2var, self.b2v, self.b2a), text="Espresso", variable=self.b2var).grid(row=1, column=0, sticky="w")
    self.b2a = Entry(self.bevmenu, bd=2, textvariable=self.b2v)
    self.b2a.grid(row=1, column=1, sticky="w")

checkbox_operation

from tkinter import NORMAL, DISABLED


def check(b1var, b1v, b1a, b2var, b2v, b2a):
    if b1var.get() == 1:
        b1a.config(state=NORMAL)
    elif b1var.get() == 0:
        b1a.config(state=DISABLED)
        b1v.set("0")
    if b2var.get() == 1:
        b2a.config(state=NORMAL)
    elif b2var.get() == 0:
        b2a.config(state=DISABLED)
        b2v.set("0")

My problem is that b1 and b2 are 2 of 16 checkboxes that will pass 3 arguments each to checkbox_operation.check. If I do it to all of them as shown in my main.py, it would mean that I have to pass 48 different arguments in each checkbox and make main.py become very unreadable.

How can I make so that b1 only passes self.b1var, self.b1v, self.b1a, b2 self.b2var, self.b2v, self.b2a and so on to checkbox_operation.check?

Sieg
  • 31
  • 5

0 Answers0