如何从Checkbutton获取变量?

时间:2019-04-23 10:13:16

标签: python python-3.x tkinter

单击按钮时我正在使用Checkbuttons获取数据,但是我的变量没有变化,我也不知道为什么。通常,它在不单击时等于0,在按下时等于1。但是它的反应就像总是不被按下一样。

def config(ticket,client,code,state,Date):
def save():
        Input_comment = T_Comment.get("1.0", END)
        print("--------------")
        print("Commentaires :", T_Comment.get("1.0", END))
        print("Remboursement :",is_Remb.get())
        print("Retour colis :", is_Return.get(),"\tChoix :",var_.get())
        print("Geste :", is_Geste.get(), "\tMontant :",e_geste.get())
        print("Avoir :", is_Avoir.get())
        print("Autres :\t", T_Autre.get("1.0", END) )
        print("Details transporteur :", "\n-Transporteur :", e_track_transp.get(), "\nNumero Tracking :", e_track.get())

        #UPDATE commentaires SAV
        connexion = sqlite3.connect('Base_SAV.db')
        curseur = connexion.cursor()
        curseur.execute("UPDATE SAV SET Commentaires_SAV= (?) WHERE ID =1", (T_Comment.get("1.0", END),))
        connexion.commit()
        curseur.close()

        #UPDATE Solutions SAV
        if  is_Remb.get() == 1:
            connexion = sqlite3.connect('Base_SAV.db')
            curseur = connexion.cursor()
            curseur.execute("UPDATE SAV SET Solutions_SAV= 'Remboursement\' WHERE ID =1")
            connexion.commit()
            curseur.close()
        if is_Return.get() == 1:
            if var_.get() == 1:
                connexion = sqlite3.connect('Base_SAV.db')
                curseur = connexion.cursor()
                curseur.execute("UPDATE SAV SET Solutions_SAV= 'Retour Colis - Client\n' WHERE ID =1")
                connexion.commit()
                curseur.close()
            elif var_.get() == 2:
                connexion = sqlite3.connect('Base_SAV.db')
                curseur = connexion.cursor()
                curseur.execute("UPDATE SAV SET Solutions_SAV= 'Retour Colis - SAV\n' WHERE ID =1")
                connexion.commit()
                curseur.close()
        if is_Geste.get() == 1:
            connexion = sqlite3.connect('Base_SAV.db')
            curseur = connexion.cursor()
            curseur.execute("UPDATE SAV SET Solutions_SAV= 'Geste commercial\n' WHERE ID =1", )
            connexion.commit()
            curseur.close()
            connexion = sqlite3.connect('Base_SAV.db')
            curseur = connexion.cursor()
            curseur.execute("UPDATE SAV SET Geste_commercial= (?) WHERE ID =1", (e_geste.get(),) )
            connexion.commit()
            curseur.close()
        if is_Geste.get() == 0:
            connexion = sqlite3.connect('Base_SAV.db')
            curseur = connexion.cursor()
            curseur.execute("UPDATE SAV SET Geste_commercial= NULL WHERE ID =1" )
            connexion.commit()
            curseur.close()
        if is_Avoir.get() == 1:
            connexion = sqlite3.connect('Base_SAV.db')
            curseur = connexion.cursor()
            curseur.execute("UPDATE SAV SET Solutions_SAV= 'Avoir\n' WHERE ID =1")
            connexion.commit()
            curseur.close()
        if is_Autre.get() == 1:
            connexion = sqlite3.connect('Base_SAV.db')
            curseur = connexion.cursor()
            curseur.execute("UPDATE SAV SET Solutions_SAV= (?) WHERE ID =1", (T_Autre.get("1.0", END),))
            connexion.commit()
            curseur.close()

        #UPDATE Details Transporteurs
        connexion = sqlite3.connect('Base_SAV.db')
        curseur = connexion.cursor()
        curseur.execute("UPDATE Commande SET Transporteur= (?) WHERE ID =1", (e_track_transp.get(),))
        connexion.commit()
        curseur.close()
        connexion = sqlite3.connect('Base_SAV.db')
        curseur = connexion.cursor()
        curseur.execute("UPDATE Commande SET Num_Tracking= (?) WHERE ID =1", (e_track.get(),))
        connexion.commit()
        curseur.close()
  ...

    config = Tk()
    config.wm_state(newstate="zoomed")
    config.title('Complément de profil')
    config.iconbitmap('MyCatSAV.ico')
    config.minsize(1920,1280)
    config.maxsize(1920,1280)
    config.pack_propagate(0)
    config.option_add('*tearOff', FALSE) # Nécessaire avec windows
    config.update()
    config.geometry(config.geometry())
        is_Remb = IntVar()
    is_Return = IntVar()
    var_ = IntVar()
    is_Geste = IntVar()
    is_Avoir = IntVar()
    is_Autre = IntVar()
    e_track_transp = StringVar()
    e_track = StringVar()
    e_geste = StringVar()

Label(my_frame,text='\n\t\t').grid(row=0, column=0)
    Label(my_frame,text='\t\t').grid(row=0, column=1)
    Label(my_frame,text='\t\t').grid(row=0, column=2)
    L_Comment = Label(my_frame, text='Commentaires SAV :', font=('Arial', 22, 'bold','underline')).grid(row=1, column=1, sticky='w')
    T_Comment = Text(my_frame, height=10, width=100,font=('Arial',14))
    T_Comment.grid(row=2, column=1,columnspan=7, sticky='w')
    L_Solution = Label(my_frame, text='Solutions SAV :', font=('Arial', 22, 'bold','underline')).grid(row=3, column=1, sticky='w')
    CB_Remb = Checkbutton(my_frame, text='Remboursement', variable=is_Remb, font=('Arial', 18,'bold'), command=save).grid(row=4, column=1, sticky='w')
    CB_Return = Checkbutton(my_frame, text='Retour colis : ', variable=is_Return, font=('Arial', 18,'bold'), command=save).grid(row=5, column=1, sticky='w')
    RB_Client = Radiobutton(my_frame, text='Client', variable=var_ , font=('Arial',18,'bold'), value=1, command=save).grid(row=5, column=2, sticky='w')
    RB_SAV = Radiobutton(my_frame, text='SAV', variable=var_, font=('Arial',18,'bold'), value=2, command=save).grid(row=5, column=3, sticky='w')
    CB_Geste = Checkbutton(my_frame, text='Geste commercial :', variable=is_Geste, font=('Arial', 18,'bold'), command=save).grid(row=6, column=1, sticky='w')
    E_Geste = Entry(my_frame, textvariable=e_geste,width=6, font=('Arial', 18,'bold'))
    E_Geste.grid(row=6, column=2, sticky='w')
    CB_Avoir = Checkbutton(my_frame, text='Avoir', variable=is_Avoir, font=('Arial', 18,'bold'), command=save).grid(row=7, column=1, sticky='w')
    CB_Autre = Checkbutton(my_frame, text='Autres : ', variable=is_Autre, font=('Arial', 18,'bold'), command=save).grid(row=8, column=1, sticky='w')
    T_Autre = Text(my_frame, width=100, height=2, font=('Arial',14))
    T_Autre.grid(row=9, column=1,columnspan=7 ,sticky='w')
    CB_Tracking = Label(my_frame, text='Details Transporteur :', font=('Arial', 22,'bold','underline')).grid(row=10, column=1,sticky='w')
    CB_Tracking_transp = Label(my_frame, text='-Transporteur :', font=('Arial',16)).grid(row=10, column=2,sticky='w')
    CB_N_tracking = Label(my_frame, text='-N° Tracking :', font=('Arial',16)).grid(row=11, column=2,sticky='w')
    E_Tracking_transp = Entry(my_frame,textvariable=e_track_transp, width=50, font=('Arial',14)).grid(row=10, column=3,columnspan=3,sticky='w')
    E_Tracking_ = Entry(my_frame,textvariable=e_track, width=50, font=('Arial',14)).grid(row=11, column=3,columnspan=3,sticky='w')
    my_frame.bind('<Return>', save_)

我单击哪个Checkbutton都没有关系,什么也没发生。会影响Checkbutton的变量保持不变,但仍等于0。但是当我键入Text时,没关系,我的问题只有Checkbutton。`

编辑:我发现了我的错误。我用了2次Tk()而不是Toplevel()

1 个答案:

答案 0 :(得分:0)

为什么不使用ttk.Checkbutton小部件?

#!/usr/bin/python3
import tkinter as tk
from tkinter import ttk
from tkinter import messagebox


class Main(ttk.Frame):
    def __init__(self, parent):
        super().__init__()

        self.parent = parent

        self.check = tk.BooleanVar()

        self.init_ui()

    def init_ui(self):

        self.pack(fill=tk.BOTH, expand=1)

        f = ttk.Frame()


        ttk.Label(f, text="Checkbutton:").pack()
        ttk.Checkbutton(f,
                       onvalue=1,
                       offvalue=0,
                       variable=self.check).pack()

        w = ttk.Frame()

        ttk.Button(w, text="Callback",command=self.on_callback).pack()
        ttk.Button(w, text="Reset", command=self.on_reset).pack()
        ttk.Button(w, text="Set", command=self.on_set).pack()
        ttk.Button(w, text="Close", command=self.on_close).pack()

        f.pack(side=tk.LEFT, fill=tk.BOTH, expand=1)
        w.pack(side=tk.RIGHT, fill=tk.BOTH, expand=1)


    def on_callback(self,):

        print ("self.check = {}".format(self.check.get()))


    def on_reset(self):

        self.check.set(0)

    def on_set(self):

        self.check.set(1)


    def on_close(self):
        self.parent.on_exit()

class App(tk.Tk):
    """Start here"""

    def __init__(self):
        super().__init__()

        self.protocol("WM_DELETE_WINDOW", self.on_exit)

        self.set_title()
        self.set_style()

        frame = Main(self,)
        frame.pack(fill=tk.BOTH, expand=1)

    def set_style(self):
        self.style = ttk.Style()
        #('winnative', 'clam', 'alt', 'default', 'classic', 'vista', 'xpnative')
        self.style.theme_use("clam")


    def set_title(self):
        s = "{0}".format('Simple App')
        self.title(s)

    def on_exit(self):
        """Close all"""
        if messagebox.askokcancel("Simple App", "Do you want to quit?", parent=self):
            self.destroy()               

if __name__ == '__main__':
    app = App()
    app.mainloop()