Python ttk 模块,Checkbutton() 实例源码

我们从Python开源项目中,提取了以下15个代码示例,用于说明如何使用ttk.Checkbutton()

项目:metlab    作者:norling    | 项目源码 | 文件源码
def __init__(self, parent, title="", optional = False, opt_func = None, *args, **options):
        Frame.__init__(self, parent, *args, **options)

        self.expanded = IntVar()
        self.expanded.set(0)

        self.enabled = IntVar()
        self.enabled.set(1)

        self.header = ttk.Frame(self)

        if optional:
            title_line = ttk.Checkbutton(self.header, text=title, variable=self.enabled, command=opt_func)
        else:
            title_line = ttk.Label(self.header, text="      %s" % (title))

        title_line.pack(side="top", fill=X)

        self.toggle_button = ttk.Checkbutton(self.header, width=2, text='+', 
                                         command=self.toggle_expanded, variable=self.expanded, style='Toolbutton')
        self.toggle_button.pack(side="left", fill=X)
        self.header.pack(fill=X, expand=1)

        self.sub_frame = Frame(self)
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def test_invoke(self):
        success = []
        def cb_test():
            success.append(1)
            return "cb test called"

        cbtn = ttk.Checkbutton(self.root, command=cb_test)
        # the variable automatically created by ttk.Checkbutton is actually
        # undefined till we invoke the Checkbutton
        self.assertEqual(cbtn.state(), ('alternate', ))
        self.assertRaises(tkinter.TclError, cbtn.tk.globalgetvar,
            cbtn['variable'])

        res = cbtn.invoke()
        self.assertEqual(res, "cb test called")
        self.assertEqual(cbtn['onvalue'],
            cbtn.tk.globalgetvar(cbtn['variable']))
        self.assertTrue(success)

        cbtn['command'] = ''
        res = cbtn.invoke()
        self.assertFalse(str(res))
        self.assertLessEqual(len(success), 1)
        self.assertEqual(cbtn['offvalue'],
            cbtn.tk.globalgetvar(cbtn['variable']))
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def _setup_widgets(self):
        themes_lbl = ttk.Label(self, text="Themes")

        themes = self.style.theme_names()
        self.themes_combo = ttk.Combobox(self, values=themes, state="readonly")
        self.themes_combo.set(themes[0])
        self.themes_combo.bind("<<ComboboxSelected>>", self._theme_sel_changed)

        change_btn = ttk.Button(self, text='Change Theme',
            command=self._change_theme)

        theme_change_checkbtn = ttk.Checkbutton(self,
            text="Change themes when combobox item is activated",
            variable=self.theme_autochange)

        themes_lbl.grid(ipadx=6, sticky="w")
        self.themes_combo.grid(row=0, column=1, padx=6, sticky="ew")
        change_btn.grid(row=0, column=2, padx=6, sticky="e")
        theme_change_checkbtn.grid(row=1, columnspan=3, sticky="w", pady=6)

        top = self.winfo_toplevel()
        top.rowconfigure(0, weight=1)
        top.columnconfigure(0, weight=1)
        self.columnconfigure(1, weight=1)
        self.grid(row=0, column=0, sticky="nsew", columnspan=3, rowspan=2)
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def test_invoke(self):
        success = []
        def cb_test():
            success.append(1)
            return "cb test called"

        cbtn = ttk.Checkbutton(self.root, command=cb_test)
        # the variable automatically created by ttk.Checkbutton is actually
        # undefined till we invoke the Checkbutton
        self.assertEqual(cbtn.state(), ('alternate', ))
        self.assertRaises(tkinter.TclError, cbtn.tk.globalgetvar,
            cbtn['variable'])

        res = cbtn.invoke()
        self.assertEqual(res, "cb test called")
        self.assertEqual(cbtn['onvalue'],
            cbtn.tk.globalgetvar(cbtn['variable']))
        self.assertTrue(success)

        cbtn['command'] = ''
        res = cbtn.invoke()
        self.assertFalse(str(res))
        self.assertLessEqual(len(success), 1)
        self.assertEqual(cbtn['offvalue'],
            cbtn.tk.globalgetvar(cbtn['variable']))
项目:ndk-python    作者:gittor    | 项目源码 | 文件源码
def test_invoke(self):
        success = []
        def cb_test():
            success.append(1)
            return "cb test called"

        cbtn = ttk.Checkbutton(command=cb_test)
        # the variable automatically created by ttk.Checkbutton is actually
        # undefined till we invoke the Checkbutton
        self.assertEqual(cbtn.state(), ('alternate', ))
        self.assertRaises(Tkinter.TclError, cbtn.tk.globalgetvar,
            cbtn['variable'])

        res = cbtn.invoke()
        self.assertEqual(res, "cb test called")
        self.assertEqual(cbtn['onvalue'],
            cbtn.tk.globalgetvar(cbtn['variable']))
        self.assertTrue(success)

        cbtn['command'] = ''
        res = cbtn.invoke()
        self.assertEqual(res, '')
        self.assertFalse(len(success) > 1)
        self.assertEqual(cbtn['offvalue'],
            cbtn.tk.globalgetvar(cbtn['variable']))
项目:heartbreaker    作者:lokori    | 项目源码 | 文件源码
def __init__(self, parent, buttontext, mycommand,r,c):
        ttk.Checkbutton.__init__(self, parent)
        self.var = BooleanVar()
        self.config(text=buttontext, onvalue= True , variable = self.var, offvalue = False, command=mycommand)
        self.grid(row=r,column=c)
项目:StochOPy    作者:keurfonluu    | 项目源码 | 文件源码
def frame1_sync(self):
        if not self.frame1.first_run:
            self.frame1.sync.forget()
        self.frame1.sync = ttk.Frame(self.frame1, borderwidth = 0)
        self.frame1.sync.place(width = 170, height = 25, relx = 0.35, y = 55, anchor = "nw")
        if self.solver_name.get() in [ "CPSO", "PSO", "DE" ]:
            # sync
            sync_button = ttk.Checkbutton(self.frame1.sync, text = "Synchronize",
                                          variable = self.sync, takefocus = False)

            # Layout
            sync_button.place(relx = 0, x =0, y = 0, anchor = "nw")
项目:oil    作者:oilshell    | 项目源码 | 文件源码
def create(self, **kwargs):
        return ttk.Checkbutton(self.root, **kwargs)
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def create(self, **kwargs):
        return ttk.Checkbutton(self.root, **kwargs)
项目:Course-Page-Downloader    作者:kp96    | 项目源码 | 文件源码
def __init__ (self, root):
        self.root = root
        root.title ("Course Page Downlaoder")
        mainframe = ttk.Frame(root, padding="3 3 12 12")
        mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
        mainframe.columnconfigure(0, weight=1)
        mainframe.rowconfigure(0, weight=1)
        self.mainframe = mainframe
        ttk.Label(mainframe, text="Registration Number").grid(row=0, column=0)
        self.regno = StringVar()
        self.pwd = StringVar()
        self.cache = IntVar()
        self.cache.set(1)
        self.progress = IntVar()
        self.progress.set(0)
        self.hint = StringVar()
        print "mama here"
        self.hint.set("Hit Download or Return to Proceed")
        self.regno_entry = ttk.Entry(self.mainframe, width=60, textvariable=self.regno)
        self.regno_entry.grid(column=0, row=1, sticky=(E))
        self.pwd_label = ttk.Label(self.mainframe, text="Password").grid(row=2, column=0)
        self.pwd_entry = ttk.Entry(self.mainframe, width=60, show='*', textvariable=self.pwd)
        self.pwd_entry.grid(column=0, row=3, sticky=(E))
        self.cb = ttk.Checkbutton(self.mainframe, text="Remember Me", variable = self.cache)
        self.cb.grid(column=0, row=4)
        self.dlbutton = ttk.Button(self.mainframe, text="Download", command= lambda: self.calculate())
        self.dlbutton.grid(column = 0, row=5)
        for child in self.mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
        self.hint_label = ttk.Label(self.mainframe, text="Press Download to Begin", justify='center')
        self.hint_label.grid(row=6)
        self.prgbar = ttk.Progressbar(self.mainframe, orient='horizontal', mode='determinate', variable=self.progress).grid(column=0, row=7)
        self.regno_entry.focus()
项目:Enrich2    作者:FowlerLab    | 项目源码 | 文件源码
def body(self, master, row, columns=DEFAULT_COLUMNS, **kwargs):
        """
        Place the required elements using the grid layout method.

        Returns the number of rows taken by this element.
        """
        self.checkbox = ttk.Checkbutton(master, text=self.text,
                                        variable=self.value)
        self.checkbox.grid(row=row, column=0, columnspan=columns, sticky="w")
        return 1
项目:rig-remote    作者:Marzona    | 项目源码 | 文件源码
def __init__(self, *args, **kwargs):
        self.var = kwargs.get('variable', tk.BooleanVar())
        kwargs['variable'] = self.var
        ttk.Checkbutton.__init__(self, *args, **kwargs)
项目:StochOPy    作者:keurfonluu    | 项目源码 | 文件源码
def frame1(self):
        self.frame1 = ttk.LabelFrame(self.master, text = "Parameters", borderwidth = 2, relief = "groove")
        self.frame1.place(bordermode = "outside", relwidth = 0.99, relheight = 0.21, relx = 0, x = 5, y = 5, anchor = "nw")
        self.frame1.first_run = True

        # function
        function_label = ttk.Label(self.frame1, text = "Function")
        function_option_menu = ttk.OptionMenu(self.frame1, self.function, self.function.get(),
                                              *sorted(self.FUNCOPT))

        # max_iter
        max_iter_label = ttk.Label(self.frame1, text = "Maximum number of iterations")
        max_iter_spinbox = Spinbox(self.frame1, from_ = 2, to_ = 9999,
                                   increment = 1, textvariable = self.max_iter,
                                   width = 6, justify = "right", takefocus = True)

        # fps
        fps_label = ttk.Label(self.frame1, text = "Delay between frames (ms)")
        fps_spinbox = Spinbox(self.frame1, from_ = 1, to_ = 1000,
                              increment = 1, textvariable = self.interval,
                              width = 6, justify = "right", takefocus = True)

        # seed
        seed_button = ttk.Checkbutton(self.frame1, text = "Fix seed",
                                      variable = self.fix_seed, takefocus = False)
        seed_spinbox = Spinbox(self.frame1, from_ = 0, to_ = self.MAX_SEED,
                               increment = 1, textvariable = self.seed,
                               width = 6, justify = "right", takefocus = True)

        # solver
        solver_label = ttk.Label(self.frame1, text = "Solver")

        solver_option_menu = ttk.OptionMenu(self.frame1, self.solver_name, self.solver_name.get(),
                                            *(self.EAOPT + self.MCOPT), command = self.select_widget)

        # constrain
        constrain_button = ttk.Checkbutton(self.frame1, text = "Constrain",
                                      variable = self.constrain, takefocus = False)

        # Layout
        function_label.place(relx = 0., x = 5, y = 5, anchor = "nw")
        function_option_menu.place(relx = 0., x = 75, y = 3, anchor = "nw")
        max_iter_label.place(relx = 0., x = 5, y = 30, anchor = "nw")
        max_iter_spinbox.place(width = 80, relx = 0., x = 220, y = 30, anchor = "nw")
        fps_label.place(relx = 0., x = 5, y = 55, anchor = "nw")
        fps_spinbox.place(width = 80, relx = 0., x = 220, y = 55, anchor = "nw")
        seed_button.place(relx = 0., x = 5, y = 80, anchor = "nw")
        seed_spinbox.place(width = 80, relx = 0., x = 220, y = 80, anchor = "nw")
        solver_label.place(relx = 0.35, x = 0, y = 5, anchor = "nw")
        solver_option_menu.place(relx = 0.35, x = 50, y = 3, anchor = "nw")
        constrain_button.place(relx = 0.35, x = 0, y = 80, anchor = "nw")
项目:python2-tracer    作者:extremecoders-re    | 项目源码 | 文件源码
def __init__(self):
        ttk.Frame.__init__(self, borderwidth=3)

        self.style = ttk.Style()

        # XXX Ideally I wouldn't want to create a Tkinter.IntVar to make
        #     it works with Checkbutton variable option.
        self.theme_autochange = Tkinter.IntVar(self, 0)
        self._setup_widgets()
项目:serialplot    作者:crxguy52    | 项目源码 | 文件源码
def AObutton(self, graphnum):
        toplvl = tk.Toplevel()
        toplvl.withdraw()
        frame = ttk.Frame(toplvl, padding=[2, 3, 3, 0])

        boxwidth = 15

        #Create the labels
        lbl = ttk.Label(frame, text='Label')
        CreateToolTip(lbl, \
        'This text will show up in the legend and the log file')
        lbl.grid(row=0, column=1)

        mult = ttk.Label(frame, text='Multiplier')
        CreateToolTip(mult, \
        'Multiply by this value')
        mult.grid(row=0, column=2)

        offset = ttk.Label(frame, text='Offset') 
        CreateToolTip(offset, \
        'Add this value. Happens AFTER the data is multiplied')
        offset.grid(row=0, column=3)  

        dashed = ttk.Label(frame, text='Dashed')
        CreateToolTip(dashed, \
        'If checked, the line will be dashed')
        dashed.grid(row=0, column=4)  

        ttk.Label(frame, text='Line 1').grid(row=1, column=0, padx=2)
        ttk.Label(frame, text='Line 2').grid(row=2, column=0, padx=2)
        ttk.Label(frame, text='Line 3').grid(row=3, column=0, padx=2) 

        for row in range(1,3+1):
            key = 'graph'+str(graphnum)+'line'+str(row)
            #Label
            ttk.Entry(frame, width=boxwidth, \
                textvariable=self.controller.TKvariables[key][0]).grid(row=row, column=1)
            #Multiplier
            ttk.Entry(frame, width=boxwidth, \
                textvariable=self.controller.TKvariables[key][4]).grid(row=row, column=2)
            #Offset
            ttk.Entry(frame, width=boxwidth, \
                textvariable=self.controller.TKvariables[key][5]).grid(row=row, column=3) 
            #Dashed
            ttk.Checkbutton(frame, onvalue='--', offvalue='-', \
                variable=self.controller.TKvariables[key][3]).grid(row=row, column=4)
        ttk.Button(frame, text='OK', command=toplvl.destroy).grid(row=5,\
            column=3, columnspan=2, sticky='ew', pady=4)

        #Center the window
        frame.grid()
        toplvl.update()
        scrwidth = toplvl.winfo_screenwidth()
        scrheight = toplvl.winfo_screenheight()
        winwidth = toplvl.winfo_reqwidth()
        winheight = toplvl.winfo_reqheight()
        winposx = int(round(scrwidth/2 - winwidth/2))
        winposy = int(round(scrheight/2 - winheight/2))
        toplvl.geometry('{}x{}+{}+{}'.format(winwidth, winheight, winposx, winposy))
        toplvl.deiconify()