Python Forum
[tkinter] not getting checkbutton value when clicked
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[tkinter] not getting checkbutton value when clicked
#1
I'm working on a larger program where I have a series of checkbuttons that will be used to determine which field to display in another frame in the window. I've got a button in one frame that is supposed to get the state of the checkbuttons in a different frame. To try to debug it I pulled everything out of the separate classes and am just trying to get my button to print the value from a single checkbutton.

import tkinter as tk
from tkinter import ttk

global currentdatabase
currentdatabase = "my_db.sqlite"

root = tk.Tk()
root.title("AnimalTrakker")
root.geometry("1024x768+0+0")
root.grid()

member_search_screen = tk.Toplevel(root)
member_search_screen.geometry("1024x768+0+0")
member_search_screen.title("Member Search")
member_search_screen.iconify()
member_search_screen.withdraw()


def DoNothing():
    tk.messagebox.showinfo(message='This is an info box that Does Nothing')
    print("testing do nothing dialog")


def GoHome():
    print("you clicked on the top home button")
    #	Add in all the other screens to hide them here
    member_search_screen.withdraw()
    # Bring up the main screen again for further input
    root.deiconify()
    root.update()

class MemberSearch:
    def __init__(self, currentdatabase):
        print("In class MemberSearch before create main frames")
        # leftsidebar section
        leftsidebar = tk.Frame(member_search_screen, borderwidth=2, relief="raised", width=40)
        leftsidebar.grid(row=0, column=0, rowspan=11, sticky=("NSEW"))
        self.gohome = ttk.Button(leftsidebar, text="Home", command=GoHome)
        self.gohome.grid(row=0, column=0)
        self.separatorbar = ttk.Separator(leftsidebar, orient="horizontal")
        self.separatorbar.grid(row=1, column=0, sticky="EW")

        self.display_options_label = tk.Label(leftsidebar, text='Display Options', anchor="w")
        self.display_options_label.grid(row=2, column=0)

        self.separatorbar = ttk.Separator(leftsidebar, orient="horizontal")
        self.separatorbar.grid(row=3, column=0, sticky="EW")

        self.display_name_val = tk.IntVar()
        self.display_name_val.set(0)
        self.display_member_name = tk.Checkbutton(leftsidebar, text="Member Name", var=self.display_name_val, width=15,
                                                  anchor="w")
        self.display_member_name.grid(row=4, column=0, sticky="W")
        centersearch = tk.Frame(member_search_screen, width=850)
        centersearch.grid(row=0, column=1, sticky="W")
        print("after gridding the top search option frame")
        select_all_value = tk.BooleanVar()
        select_all_value.set(False)
        select_all_checkbox = tk.Checkbutton(centersearch, text='Select All Members Shown', var=select_all_value)
        select_all_checkbox.grid(row=7, column=0, rowspan=2, columnspan=2, sticky="W")
        result = tk.BooleanVar()
        search_members_button = ttk.Button(centersearch, text="Search", command=lambda: self.get_display_options_selected(result), state='active')
        search_members_button.grid(row=7, column=5, columnspan=2, pady=15)

        print (f"Display Option Name is {result.get()}")

    def get_display_options_selected(self,result):
        self.result = result
        print("inside def before getting value", result.get())
        self.result = self.display_name_val.get()
        print("inside def after getting value", result.get())
        return (self.result)

    def show_membersearch(self, currentdatabase):
        root.withdraw()
        member_search_screen.update()
        print("after screen.update in show_membersearch")
        member_search_screen.deiconify()
        print("after deiconify in show_membersearch")

class MainScreen():
    def __init__(self, currentdatabase):
        print("In class main screen")
        #	Create the choose action sidebar that will go to specific screens
        leftsidebar = tk.Frame(root, borderwidth=2, relief="raised", height=768)
        leftsidebar.grid(row=0, column=0, rowspan=8, sticky=("NSEW"))

        #	Populate the side bar with the choices
        self.home_label = tk.Label(leftsidebar, text="AnimalTrakker Registry")
        self.home_label.grid(row=0, column=0)
        self.choose_action = ttk.Treeview(leftsidebar, height=20)
        self.choose_action.grid(row=1, column=0)
        self.choose_action.title = 'AnimalTrakker Home'
        self.choose_action.insert('', 0, 'animals', text='Animals', open='false')
        self.choose_action.insert('animals', 'end', 'animalsearch', text='Animal Search')
        self.choose_action.insert('animals', 'end', 'animaladd', text='Add/Edit Animal')
        self.choose_action.insert('animals', 'end', 'animalwebentry', text='Process Web Entries')
        self.choose_action.insert('animals', 'end', 'animalreports', text='Animal Reports')
        self.choose_action.insert('animals', 'end', 'animaldeaths', text='Animal Deaths')
        self.choose_action.insert('animals', 'end', 'animaltransfers', text='Animal Transfers')
        self.choose_action.insert('', 1, 'members', text='Members', open='true')
        self.choose_action.insert('members', 'end', 'membersearch', text='Member Search')
        self.choose_action.insert('members', 'end', 'memberadd', text='Add/Edit Member')
        self.choose_action.insert('members', 'end', 'memberreports', text='Member Reports')
        self.choose_action.insert('', 2, 'flockherdbook', text='Flock/Herd Book', open='false')
        self.choose_action.insert('flockherdbook', 'end', 'printregistrations', text='Print Registrations')
        self.choose_action.insert('flockherdbook', 'end', 'printtransfers', text='Print Transfers')
        self.choose_action.insert('flockherdbook', 'end', 'printmembers', text='Print Members')
        self.choose_action.insert('flockherdbook', 'end', 'printprefix', text='Print Prefixes')
        self.choose_action.insert('', 3, 'populationanalysis', text='Population Analysis', open='false')
        self.choose_action.insert('populationanalysis', 'end', 'definefounders', text='Define Founders')
        self.choose_action.insert('populationanalysis', 'end', 'calculateinbreeding', text='Calculate Inbreeding')
        self.choose_action.insert('populationanalysis', 'end', 'calculatebloodlines', text='Calculate Bloodlines')
        self.choose_action.insert('populationanalysis', 'end', 'popanalysis_item', text='Next Pop Analysis')
        self.choose_action.insert('', 4, 'registrysetup', text='Registry Setup', open='false')
        self.choose_action.insert('registrysetup', 'end', 'choosedatabase', text='Choose Database')
        self.choose_action.insert('registrysetup', 'end', 'general_setup', text='General Defaults')
        self.choose_action.insert('registrysetup', 'end', 'setup_item2', text='Next Setup2')
        # self.choose_action.config(height=25)
        self.choose_action.bind("<Double-1>", self.OnDoubleClick)


    def OnDoubleClick(self, event):
        item = self.choose_action.selection()[0]
        # print("In OnDoubleClick and you clicked on", self.choose_action.item(item, "text"))
        # print(item)
        # add all the options in here
        if item == "animalsearch":
            pass
        elif item == "membersearch":
            MemberSearch.show_membersearch(self, currentdatabase)
        elif item == "memberreports":
            pass
            # MemberReports.show_memberreports(self, currentdatabase)
        elif item == "choosedatabase":
            print("should handle change database here")
        elif item == "memberadd":
            pass
            # MemberAddEdit.show_memberaddedit(self, currentdatabase)
        else:
            print("fell out end of what to do loop")


lt_top = MainScreen(currentdatabase)
lt_member_search = MemberSearch(currentdatabase)

root.mainloop()
What I expect to happen is that I select member search by double clicking. I get the next top Window displayed. Then I click the search button without selecting the checkbutton for name in the leftsidebar. I should get printouts that say the display_name_var is false. Which I do.

Output:
In class main screen In class MemberSearch before create main frames after gridding the top search option frame Display Option Name is False after screen.update in show_membersearch after deiconify in show_membersearch after screen.update in show_membersearch after deiconify in show_membersearch inside def before getting value False inside def after getting value False you clicked on the top home button
Then if I do select the checkbutton it shows up checked in the left sidebar. I then go and click the search again and I expected to get a a False in the inside def print string (because it's before I dod the .get to get the value of the checkbutton) and a True in the after getting value print string but that is not what I get I get the same results as before. Always False.

Can anyone point me to an example that does this sort of task? Even better if the frames are in their own class as that is what I will need to do eventually.

Thanks
Reply


Messages In This Thread
[tkinter] not getting checkbutton value when clicked - by OogieM - Sep-19-2020, 09:05 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [PyQt] Button clicked not working catlessness 10 8,085 Oct-21-2021, 12:36 PM
Last Post: catlessness
  [Tkinter] Hide clicked buttons Rubberduck 6 3,446 Jun-02-2021, 12:44 PM
Last Post: Rubberduck
  Checkbutton writing selection to file blakefindlay 1 2,078 Jan-28-2021, 01:56 PM
Last Post: deanhystad
Question [Tkinter] Checkbutton clicks events does not update visually. nicolaask 1 2,919 Dec-20-2020, 06:11 PM
Last Post: nicolaask
  [Tkinter] How to insert 'Checkbutton' to 'Treeview' and associate each item? water 2 12,996 Dec-19-2020, 05:24 PM
Last Post: water
  Find Checkbutton State PEGylated_User 3 3,048 Oct-23-2020, 12:43 AM
Last Post: Larz60+
  [PyQt] Avoid clicked event from button when button is not physically selected and clicked mart79 2 2,309 May-05-2020, 12:54 PM
Last Post: mart79
  [Tkinter] ttk.Checkbutton set on/off ifigazsi 5 10,305 Apr-04-2020, 07:34 PM
Last Post: deanhystad
  [Tkinter] Displaying Data from a database and run a function when clicked? PythonNPC 1 2,024 Mar-11-2020, 08:16 PM
Last Post: Larz60+
  tkinter checkbutton if checked MC2020 2 5,925 Jan-21-2020, 07:08 PM
Last Post: joe_momma

Forum Jump:

User Panel Messages

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