设置tkinter复选框默认值的多帧

2024-04-23 14:16:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将我的单窗口tkinter GUI逐步移动到多窗口。我试图将名为checkb_ult_tubo的复选框的默认值设置为1(已选中),但尽管我将变量设置为1,默认情况下该复选框仍处于关闭状态。 我甚至尝试在Page5X类块中清除一个新变量(tk.IntVar),并将其设置为1,但复选框仍然显示

这是我的密码


class Keep(tk.Tk):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)

        self.shared_data ={
            "rvo": tk.StringVar(),
            'num_racks': tk.StringVar(),
            'num_falcons': tk.StringVar(),
            'first_tip': tk.StringVar(),
            'ot_2_ip': tk.StringVar(),
            'last_tube': tk.StringVar(),
            'rack_completo_check' : tk.IntVar()
        }

        self.frames = {
            'StartPage': StartPage(self, self),
            '5x': Page5X(self, self),
            '40x': Page40X(self, self),
            'nfw': PageNFW(self, self),
            'pc': PagePC(self, self),

        }

        self.current_frame = None
        self.show_frame('StartPage')


    def show_frame(self, name):
        if self.current_frame:
            self.current_frame.forget()
        self.current_frame = self.frames[name]
        self.current_frame.pack()

        self.current_frame.update_widgets() # <-- update data in widgets


class StartPage(tk.Frame):

    def __init__(self, parent, controller):
        super().__init__(parent)
        self.controller = controller

        label_rvo = tk.Label(self, text='Reactivo a alicuotar:')
        label_rvo.pack()

        rvo = self.controller.shared_data["rvo"]

        rb_rvo1 = tk.Radiobutton(
            master=self,
            text='Master Mix 5x',
            value='5x',
            variable=rvo)

        rb_rvo1.pack(padx=3, pady=2)

        rb_rvo2 = tk.Radiobutton(
            master=self,
            text='RT Mix 40x',
            value='40x',
            variable=rvo)

        rb_rvo2.pack(padx=3, pady=2)

        rb_rvo3 = tk.Radiobutton(
            master=self,
            text='Nuclease Free Water',
            value='nfw',
            variable=rvo)

        rb_rvo3.pack(padx=3, pady=2)

        rb_rvo4 = tk.Radiobutton(
            master=self,
            text='Positive Control',
            value='pc',
            variable=rvo)

        rb_rvo4.pack(padx=3, pady=2)


        button = tk.Button(self, text="Siguiente", command=self.next_page)
        button.pack()

    def update_widgets(self):
        rvo = self.controller.shared_data["rvo"].get()

    def next_page(self):
        rvo = self.controller.shared_data["rvo"].get()
        self.controller.show_frame(rvo)

class Page5X(tk.Frame):

    def __init__(self, parent, controller):
        super().__init__(parent)
        self.controller = controller


        ################## SELECCION DEL ULTIMO TUBO ##################

        sub_frame2 = tk.Frame(self)


        def disable_enable_button():
            if boton_ult_tubo["state"] == "normal":
                boton_ult_tubo["state"] = "disabled"
            else:
                boton_ult_tubo["state"] = "normal"

        def popup_select_tube():

            last_tube = self.controller.shared_data["last_tube"]

            def guardar_seleccion_tubo():
                entry_ult_tubo.configure(state='normal')
                entry_ult_tubo.delete(0, tk.END)
                entry_ult_tubo.insert(0, last_tube.get())
                entry_ult_tubo.configure(state='readonly')
                popup.destroy()

            popup = tk.Toplevel(self)
            popup.wm_title("Seleccion del ultimo tubo")

            label_tips = tk.Label(popup, text='Seleccione el ultimo tubo disponible:')
            label_tips.grid(row=1, column=1, columnspan=12, padx=10, pady=10)

            for i in range(8):
                label_tips = tk.Label(popup, text=str(i + 1))
                label_tips.grid(row=2, column=2 + i, padx=10, pady=10)

            for j in range(5):
                label_tips = tk.Label(popup, text=string.ascii_uppercase[j])
                label_tips.grid(row=3 + j, column=1, padx=10, pady=10)

            tips_list = []
            for i in range(8):
                for j in range(5):
                    tip = tk.Radiobutton(
                        master=popup,
                        value=string.ascii_uppercase[j] + str(i + 1),
                        variable=last_tube)

                    tips_list.append(tip)

                    tip.grid(row=3 + j, column=2 + i, padx=10, pady=10)

            B1 = ttk.Button(popup, text="Guardar seleccion", command=guardar_seleccion_tubo)
            B1.grid(row=11, column=1, columnspan=12, padx=10, pady=10)

            popup.resizable(False, False)
            popup.mainloop()



        checkb_ult_tubo = tk.Checkbutton(sub_frame2,
                                         text="Ultimo rack completo",
                                         variable=self.controller.shared_data['rack_completo_check'],
                                         height=1,
                                         width=15,
                                         command=disable_enable_button,
                                         onvalue=0, offvalue=1)

        self.controller.shared_data['rack_completo_check'].set(1)


        checkb_ult_tubo.grid(row=1, column=1, columnspan=2, padx=3, pady=3)

        label_ult_tubo2 = tk.Label(sub_frame2, text='Ultimo tubo:')
        label_ult_tubo2.grid(row=2, column=1, columnspan=2, padx=3, pady=3)

        entry_ult_tubo = tk.Entry(sub_frame2, width=4)
        entry_ult_tubo.insert(0, 'E8')
        entry_ult_tubo.configure(state='readonly')
        entry_ult_tubo.grid(row=3, column=1, padx=3, pady=3)

        boton_ult_tubo = tk.Button(sub_frame2, text="Seleccionar", state='disable', command=popup_select_tube)
        boton_ult_tubo.grid(row=3, column=2, columnspan=1, padx=10, pady=3)

        sub_frame2.pack()


    def guardar(self):
        self.controller.shared_data["num_racks"] = menu_num_racks.get()
        self.controller.shared_data["num_falcons"] = menu_num_racks.get()


    def update_widgets(self):
        rack_completo_check = self.controller.shared_data['rack_completo_check'].get()

class Page40X(tk.Frame):

    def __init__(self, parent, controller):
        super().__init__(parent)
        self.controller = controller

        self.label = tk.Label(self, text="") # <-- create empty label
        self.label.pack()


    def update_widgets(self):
        rvo = self.controller.shared_data["rvo"].get()
        self.label["text"] = rvo

class PageNFW(tk.Frame):

    def __init__(self, parent, controller):
        super().__init__(parent)
        self.controller = controller

        self.label = tk.Label(self, text="") # <-- create empty label
        self.label.pack()


    def update_widgets(self):
        rvo = self.controller.shared_data["rvo"].get()
        self.label["text"] = rvo

class PagePC(tk.Frame):

    def __init__(self, parent, controller):
        super().__init__(parent)
        self.controller = controller

        self.label = tk.Label(self, text="") # <-- create empty label
        self.label.pack()


    def update_widgets(self):
        rvo = self.controller.shared_data["rvo"].get()
        self.label["text"] = rvo

if __name__ == "__main__":
    keep = Keep()
    keep.mainloop()


1条回答
网友
1楼 · 发布于 2024-04-23 14:16:15

所以我注意到checkb_ult_tubo中的onvalue是0,offvalue是1。我认为当您将复选框的值设置为1时,它保持关闭状态,因为offvalue为1。试试这个:

        checkb_ult_tubo = tk.Checkbutton(sub_frame2,
                                     text="Ultimo rack completo",
                                     variable=self.controller.shared_data['rack_completo_check'],
                                     height=1,
                                     width=15,
                                     command=disable_enable_button,
                                     onvalue=1, offvalue=0)

相关问题 更多 >