Python Forum
[Tkinter] Can't seem to get the current state of a checkbutton.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] Can't seem to get the current state of a checkbutton.
#1
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.
Reply
#2
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):
Reply
#3
This link has an example of how to do it https://www.tutorialspoint.com/wxpython/..._class.htm
Reply
#4
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.
Reply
#5
Quote:I actually learned about classes, and code structuring yesterday
using classes for GUI code is (in my opinion) the only way to do.
Reply
#6
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?
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Checkbutton writing selection to file blakefindlay 1 2,079 Jan-28-2021, 01:56 PM
Last Post: deanhystad
Question [Tkinter] Checkbutton clicks events does not update visually. nicolaask 1 2,920 Dec-20-2020, 06:11 PM
Last Post: nicolaask
  [Tkinter] How to insert 'Checkbutton' to 'Treeview' and associate each item? water 2 13,000 Dec-19-2020, 05:24 PM
Last Post: water
  Find Checkbutton State PEGylated_User 3 3,052 Oct-23-2020, 12:43 AM
Last Post: Larz60+
  [tkinter] not getting checkbutton value when clicked OogieM 5 5,893 Sep-20-2020, 04:49 PM
Last Post: deanhystad
  [Tkinter] ttk.Checkbutton set on/off ifigazsi 5 10,310 Apr-04-2020, 07:34 PM
Last Post: deanhystad
  tkinter checkbutton if checked MC2020 2 5,925 Jan-21-2020, 07:08 PM
Last Post: joe_momma
  [Tkinter] Create a set of checkbutton widgets and refer to every one of them individually? Mariano 1 2,677 Apr-11-2019, 06:20 PM
Last Post: woooee
  doing something after been checked a checkbutton gray 1 4,236 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