Python Forum
Find Checkbutton State
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Find Checkbutton State
#3
I have seen one way is to create a dictionary and save the name as key and an IntVar as value, the checked box is 1.
here's a rewrite of your code hope it points you on path to success:
from tkinter import *
from tkinter import ttk
 
 
class Stability_Label_Creator():
    def __init__(self, master):
         
        self.notebook = ttk.Notebook(master)
        self.notebook.pack()
        self.checkbuttons= {}
 
        # Add tabs to main window
        self.formulations = ttk.Frame(self.notebook)
        self.stabilityschedule = ttk.Frame(self.notebook)
        self.notebook.add(self.formulations, text = 'Formulations')
        self.notebook.add(self.stabilityschedule, text = 'Stability Schedule')
         
        ttk.Label(self.formulations, text = 'Formulation Name').grid(row = 0, column = 3)
        ttk.Label(self.formulations, text = 'Formulation Lot Number').grid(row = 0, column = 4)
        for x in range(1, 26):
            RowLabel = 'RowLabel' + str(x)
            RowLabel = ttk.Label(self.formulations, text = x)
            RowLabel.grid(row = x, column = 1)
 
            FormulationVar = 'FormulationVar' + str(x)
            value_= IntVar()
            self.checkbuttons[FormulationVar]= value_
            self._Formulation = 'Formulation' + str(x)
            self._Formulation = ttk.Checkbutton(self.formulations, text = FormulationVar, variable = value_,
                                                )
            self._Formulation.grid(row = x, column = 2)
            
 
            FormulationName = 'FormulationName' + str(x)
            FormulationName = ttk.Entry(self.formulations, width = 30)
            FormulationName.grid(row = x, column = 3)  
 
            FormulationLot = 'FormulationLot' + str(x)
            FormulationLot = ttk.Entry(self.formulations, width = 30)
            FormulationLot.grid(row = x, column = 4)
 
        self.NextButton = ttk.Button(self.formulations, text = 'Next', command = self.Next)
        self.NextButton.grid(row = 26, column = 4)
 
        ttk.Label(self.stabilityschedule, text = 'Time Point').grid(row = 0, column = 2)
        ttk.Label(self.stabilityschedule, text = 'Storage Conditions').grid(row = 0, column = 3, columnspan = 7)
        ttk.Label(self.stabilityschedule, text = 'Vials Per Time Point').grid(row = 0, column = 10)
        self.BackButton = ttk.Button(self.stabilityschedule, text = 'Back', command = self.Back)
        self.BackButton.grid(row = 26, column = 9)
        self.FinishedButton = ttk.Button(self.stabilityschedule, text = 'Finished', command = self.Finished)
        self.FinishedButton.grid(row = 26, column = 10)
        for x in range(1, 26):
            TimePointLabel = 'TimePointLabel' + str(x)
            TimePointLabel = ttk.Label(self.stabilityschedule, text = x)
            TimePointLabel.grid(row = x, column = 1)
 
            TimePoint = 'TimePoint' + str(x)
            TimePoint = ttk.Entry(self.stabilityschedule, width = 30)
            TimePoint.grid(row = x, column = 2) 
 
            VialsPerTimePoint = 'VialsPerTimePoint' + str(x)
            VialsPerTimePoint = ttk.Entry(self.stabilityschedule, width = 30)
            VialsPerTimePoint.grid(row = x, column = 10) 
 
        # Need to get the Check Box Text to be correct.  Right now they all say 60C
        StorCond = ['-20C', '5C', '25C', '30C', '40C', '50C', '60C']
        for x in range(1, 26):
            for y in range(3, 10):
                # for y in range(3, 10):
                StorageConditionsVar = 'StorageConditionsVar' + str(StorCond[y-3])
                StorageConditionsVar = BooleanVar()
                StorageConditions = 'StorageConditions' + str(StorCond[y-3])
                self.StorageConditions = ttk.Checkbutton(self.stabilityschedule, name = 'storageConditions' + str(StorCond[y-3]) + str(x),
                    text = str(StorCond[y-3]), variable = StorageConditionsVar, onvalue = True, offvalue = False)
                StorageConditionsVar.set(False)
                self.StorageConditions.grid(row = x, column = y)
 
                # print(self.StorageConditions.state())
     
    def Finished(self):
        print('Finished Button Clicked')
        for key,value in self.checkbuttons.items():
            if value.get():
                print(key) #prints the name of the checked box
     
    def Next(self):
        # Moves to next tab in notebook
        self.notebook.select(1)
        
 
    def Back(self):
        # Returns to first tab in notebook
        self.notebook.select(0)
     
    
 
def main():            
     
    mainwindow = Tk()
    mainwindow.title('Stability Label Creator')
    app = Stability_Label_Creator(mainwindow)
    mainwindow.mainloop()
 
if __name__ == "__main__":
    main()
Reply


Messages In This Thread
Find Checkbutton State - by PEGylated_User - Oct-21-2020, 02:58 PM
RE: Find Checkbutton State - by deanhystad - Oct-21-2020, 03:29 PM
RE: Find Checkbutton State - by joe_momma - Oct-21-2020, 10:30 PM
RE: Find Checkbutton State - by Larz60+ - Oct-23-2020, 12:43 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Checkbutton writing selection to file blakefindlay 1 2,150 Jan-28-2021, 01:56 PM
Last Post: deanhystad
Question [Tkinter] Checkbutton clicks events does not update visually. nicolaask 1 3,076 Dec-20-2020, 06:11 PM
Last Post: nicolaask
  [Tkinter] How to insert 'Checkbutton' to 'Treeview' and associate each item? water 2 13,408 Dec-19-2020, 05:24 PM
Last Post: water
  [tkinter] not getting checkbutton value when clicked OogieM 5 6,282 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  [Tkinter] ttk.Checkbutton set on/off ifigazsi 5 10,681 Apr-04-2020, 07:34 PM
Last Post: deanhystad
  tkinter checkbutton if checked MC2020 2 6,078 Jan-21-2020, 07:08 PM
Last Post: joe_momma
  [Tkinter] Can't seem to get the current state of a checkbutton. p_hobbs 6 3,445 Nov-07-2019, 11:38 PM
Last Post: p_hobbs
  [Tkinter] Create a set of checkbutton widgets and refer to every one of them individually? Mariano 1 2,734 Apr-11-2019, 06:20 PM
Last Post: woooee
  doing something after been checked a checkbutton gray 1 4,332 Feb-18-2017, 05:11 PM
Last Post: Axel_Erfurt

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020