Python Forum

Full Version: Can't seem to get the current state of a checkbutton.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello everyone. I'm pretty new to Python, so this is probably basic. I have tried about a million different things, and have gone though everything I can find on google, but can't seem to get this to work. I've got a data entry gui I'm working on, and am trying to check the state of a checkbutton, and can't seem to get it to work. Here's what I've got:

def efs_form():
    efs_arr_Time = Entry(efs_ef, width=18)
    efs_arr_Time.grid(row=1, column=1, padx=(1, 5), pady=5)

#
#   ...
#

    global chb_var
    
    chb_var = IntVar()
    
    # Average Checkbox
    efs_avg_chb = Checkbutton(efs_ef,
                          text="Include in Average",
                          variable=chb_var,
                          onvalue=1,
                          offvalue=0,
                          bg='black',
                          fg='Light Blue',
                          selectcolor='black',
                          font=("Comic Sans MS", 8, "bold"))
    efs_avg_chb.grid(row=3, column=1, pady=(20, 10), sticky=W)
    efs_avg_chb.toggle()
    
    # Submit Button
    efs_sub_btn = Button(efs_ef, text="Submit", command=sub_efs_data, width=18, bg="dark blue", fg="light blue")
    efs_sub_btn.grid(row=8, column=4, pady=50, sticky=W)
def sub_efs_data():
    db_conn = sqlite3.connect('EFS_Samples.db')
    c = db_conn.cursor()
    
    if chb_var.get():
        cu_avg = "Checked"
    else:
        cu_avg = "Unchecked"
    
    efs_arr_Time.delete(0, END)
    efs_arr_Time.insert(0, cu_avg)
For some reason, I can only get it to say "Unchecked". Any suggestions would be greatly appreciated.
the value should be in chb, and should be 1 or 0
I think that you also have to change the function definition to:
def sub_efs_data(event):
This link has an example of how to do it https://www.tutorialspoint.com/wxpython/..._class.htm
Thanks guys. I will mess with it today, and see what I can come up with. I actually learned about classes, and code structuring yesterday, so I think I'm going to be spending a big portion of time going back and restructuring my code. I will update this with my results as soon as I get to it.
Quote:I actually learned about classes, and code structuring yesterday
using classes for GUI code is (in my opinion) the only way to do.
okay @p_hobbs I did not see in your example what GUI library you were using if any -- i see someone pointed you to wxpython which is one of these GUI libraries -- is that the one you were using?
(Nov-07-2019, 05:53 PM)Denni Wrote: [ -> ]okay @p_hobbs I did not see in your example what GUI library you were using if any -- i see someone pointed you to wxpython which is one of these GUI libraries -- is that the one you were using?

Sorry, I am using tkinter.