jeudi 19 août 2021

Why do I get the address instead of value when I would like get the status of a Checkbox - tk.IntVar

I would like to get the status of the checkbox by clicking on a "Save" button and then append the key & value to a new dictionary if the status of the checkbox 1(row by row) is. I have already tried to call the dictionary with a ".get" function but it did not work. Any idea how I can solve this issue?

import tkinter as tk
from tkinter import ttk
from tkinter import *
class ToolTip(object):
    def __init__(self, widget):
        self.widget = widget
        self.tip_window = None

    def show_tip(self, tip_text):
        "Display text in a tooltip window"
        if self.tip_window or not tip_text:
            return
        x, y, _cx, cy = self.widget.bbox("insert")  # get size of widget
        x = x + self.widget.winfo_rootx() + 25  # calculate to display tooltip
        y = y + cy + self.widget.winfo_rooty() + 25  # below and to the right
        self.tip_window = tw = tk.Toplevel(self.widget)  # create new tooltip window
        tw.wm_overrideredirect(True)  # remove all Window Manager (wm) decorations
        #         tw.wm_overrideredirect(False)                 # uncomment to see the effect
        tw.wm_geometry("+%d+%d" % (x, y))  # create window size

        label = tk.Label(tw, text=tip_text, justify=tk.LEFT,
                     background="#ffffe0", relief=tk.SOLID, borderwidth=1,
                     font=("tahoma", "8", "normal"))
        label.pack(ipadx=1)

    def hide_tip(self):
        tw = self.tip_window
        self.tip_window = None
        if tw:
            tw.destroy()

def create_ToolTip(widget, text):
    toolTip = ToolTip(widget)  # create instance of class

    def enter(event):
        toolTip.show_tip(text)

    def leave(event):
        toolTip.hide_tip()

    widget.bind('<Enter>', enter)  # bind mouse events
    widget.bind('<Leave>', leave)


class OOP(tk.Frame):
    def __init__(self, parent):  # Initializer method

        tk.Frame.__init__(self, parent)

        self.canvas = tk.Canvas(self, borderwidth=0, background="#ffffff")
        self.frame = tk.Frame(self.canvas, background="#ffffff")
        self.vsb = tk.Scrollbar(self, orient="vertical", command=self.canvas.yview)
        self.canvas.configure(yscrollcommand=self.vsb.set)
        self.hsb = tk.Scrollbar(self, orient="horizontal", command=self.canvas.xview)
        self.canvas.configure(xscrollcommand=self.hsb.set)

        self.vsb.pack(side="right", fill="y")
        self.hsb.pack(side="bottom", fill="x")
        self.canvas.pack(side="left", fill="both", expand=True)
        self.canvas_window = self.canvas.create_window((4,4), window=self.frame, anchor="nw", tags="self.frame")

        self.frame.bind("<Configure>", self.onFrameConfigure)#Bind an event whenever the size of the frame changes
        self.canvas.bind("<Configure>", self.onFrameConfigure)#Enables scrolling

        self.colors = {"f1_Blue": 0, "f2_Red": 0, "f3_Green": 0, "f4_Black": 0, "f5_White": 0, "f6_Purple": 0}

        self.tabControl = ttk.Notebook(self.frame, width=2500, height=1100)  # Create Tab Control
        self.tab_menu = ttk.Frame(self.tabControl)  # Create a tab
        self.tab_menu.pack(fill=BOTH, expand=1)
        self.tabControl.add(self.tab_menu, text="MENU")  # Add a MENU tab
        self.tabControl.pack(expand=1, fill="both")  # Pack to make visible


        self.label1 = tk.Label(self.tab_menu, text=' Status ', fg='white', bg='black')
        self.mighty = ttk.LabelFrame(self.tab_menu, labelwidget=self.label1)
        self.mighty.grid(column=0, row=0, padx=20, pady=10)

        self.comp_add_drop_status = self.check_box_addordrop(1, 0, self.mighty, self.colors)

        ttk.Label(self.mighty, text="Save the colors?", foreground="blue").grid(column=0, row=15, sticky=tk.W, columnspan=3)
        self.action = ttk.Button(self.mighty, text="Save", command=self.save_colors2dict)
        self.action.grid(column=5, row=15, sticky=tk.E, columnspan=3)

        # Add a title
        win.title("TESTER")
        win.resizable(1, 1)

    def onFrameConfigure(self, event):
        self.canvas.configure(scrollregion=self.canvas.bbox("all")) #Reset the scroll region to encompass the inner frame



    def onCanvasConfigure(self, event):
        canvas_width = event.width
        self.canvas.itemconfig(self.canvas_window, width=canvas_width) #Whenever the size of the canvas changes, alter the the window region respectively


    def check_box_addordrop(self, r, c, frame, items):
        cow = c
        row = r
        self.my_list_test = []
        for item in items:
            items[item] = tk.IntVar(value=items[item])
            checkbox = tk.Checkbutton(frame, text=item[3:15], variable=items[item])
            checkbox.grid(column=cow, row=row, sticky=tk.W)
            row += 1
        return items

    def save_colors2dict(self):
        #print(list(self.comp_add_drop_basisBN.state()))
        print(self.comp_add_drop_status.values())
        print(self.comp_add_drop_status.keys())
        print(self.comp_add_drop_status.items())
        print(self.comp_add_drop_status.get("f1_Blue"))
        print(self.comp_add_drop_status.get("f2_Red"))
        print(self.comp_add_drop_status.get("f3_Green"))


if __name__ == '__main__':
    win = tk.Tk()
    example = OOP(win)
    example.pack(fill="both", expand=True)
    win.mainloop()



Aucun commentaire:

Enregistrer un commentaire