我们从Python开源项目中,提取了以下19个代码示例,用于说明如何使用ttk.Combobox()。
def _setup_widgets(self): themes = list(self.style.theme_names()) themes.insert(0, "Pick a theme") # Create a readonly Combobox which will display 4 values at max, # which will cause it to create a scrollbar if there are more # than 4 values in total. themes_combo = ttk.Combobox(self, values=themes, state="readonly", height=4) themes_combo.set(themes[0]) # sets the combobox value to "Pick a theme" # Combobox widget generates a <<ComboboxSelected>> virtual event # when the user selects an element. This event is generated after # the listbox is unposted (after you select an item, the combobox's # listbox disappears, then it is said that listbox is now unposted). themes_combo.bind("<<ComboboxSelected>>", self._change_theme) themes_combo.pack(fill='x') self.pack(fill='both', expand=1)
def create(self, **kwargs): return ttk.Combobox(self.root, **kwargs)
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)
def main(): app = App() app.master.title("Ttk Combobox") app.mainloop()
def autocomplete(self, delta=0): """autocomplete the Combobox, delta may be 0/1/-1 to cycle through possible hits""" if delta: # need to delete selection otherwise we would fix the current position self.delete(self.position, tkinter.END) else: # set position to end so selection starts where textentry ended self.position = len(self.get()) # collect hits _hits = [] for element in self._completion_list: box_value = self.get() if element.lower().startswith(box_value.lower()): # Match case insensitively _hits.append(element) # if we have a new hit list, keep this in mind if _hits != self._hits: self._hit_index = 0 self._hits=_hits # only allow cycling if we are in a known hit list if _hits == self._hits and self._hits: self._hit_index = (self._hit_index + delta) % len(self._hits) # now finally perform the auto completion if self._hits: txt=self._hits[self._hit_index] self.delete(0,tkinter.END) self.insert(0,txt) self.select_range(self.position,tkinter.END) self.event_generate('<<ComboboxSelected>>') else: self.event_generate('<<NoHits>>')
def setUp(self): support.root_deiconify() self.combo = ttk.Combobox()
def __init__(self, parent, controller): ttk.Frame.__init__(self, parent) self.controller = controller self['padding'] = [0, 7, 0, 0] #Populate the serial configuration tab self.baudlist = (4800, 9600, 19200, 38400, 57600, 115200, 230400, 921600) self.databitslist = (7, 8) self.stopbitslist = (1, 2) self.paritylist = ('None', 'Even', 'Odd', 'Mark', 'Space') baudlabel = ttk.Label(self, text='Baudrate') baudbox = ttk.Combobox(self, width=8, values=self.baudlist, textvariable=self.controller.TKvariables['baud']) datalabel = ttk.Label(self, text='Data bits') databox = ttk.Combobox(self, width=8, values = self.databitslist, \ textvariable=self.controller.TKvariables['databits']) stopbitslabel = ttk.Label(self, text='Stop bits') stopbitsbox = ttk.Combobox(self, width=8, values=self.stopbitslist, \ textvariable=self.controller.TKvariables['stopbits']) paritylabel = ttk.Label(self, text='Parity') paritybox = ttk.Combobox(self, width=8, values=self.paritylist, \ textvariable=self.controller.TKvariables['parity']) #ttk.Label(self, text=' ').grid(row=1, column=0) baudlabel.grid(row=1, column = 1, padx=5) baudbox.grid(row=1, column=2, padx=5) datalabel.grid(row=2, column = 1, padx=5) databox.grid(row=2, column=2, padx=5) stopbitslabel.grid(row=3, column = 1, padx=5) stopbitsbox.grid(row=3, column=2, padx=5) paritylabel.grid(row=4, column = 1, padx=5) paritybox.grid(row=4, column=2, padx=5)
def _create_local_pane(self, full_pane): local_pane = tk.LabelFrame(full_pane, text='Test Results') self.choose_results_button = tk.Button( local_pane, text='Results', width=15, command=self._load_run_results) self.choose_results_button.grid( row=0, column=0, sticky='ew', padx=10, pady=5) self.choose_results_entry = tk.Entry( local_pane, state='disabled', textvariable=self.runresultsvar) self.choose_results_entry.grid( row=0, column=1, sticky='nsew', padx=10, pady=5) self.choose_parser = ttk.Combobox( local_pane, show='', state='disabled') self.choose_parser.bind( '<<ComboboxSelected>>', self._on_parser_changed) self.choose_parser.grid( row=1, column=0, columnspan=2, sticky='nsew', padx=10, pady=7) self.runresultsview = TestResultsView( local_pane, on_selected=self._on_test_result_selected) self.runresultsview.grid( row=2, column=0, columnspan=2, sticky='nsew', padx=10, pady=5) self.runresultsview.rowconfigure(0, weight=1) self.runresultsview.columnconfigure(0, weight=1) local_pane.rowconfigure(2, weight=1) local_pane.columnconfigure(1, weight=1) local_pane.config(padx=10) return local_pane
def _make_combo(self, frame, text): tk.Label(frame, text='{}:'.format(text)).pack(side=tk.TOP) cbo = ttk.Combobox(frame, width=16, show='') cbo.pack(side=tk.TOP, padx=10, fill=tk.BOTH) cbo.bind('<Return>', self.check_password) cbo['values'] = self.history.get(text.lower(), []) if cbo['values']: cbo.set(cbo['values'][-1]) return cbo
def button_state(self): """ sets buttons to the correct state """ # # This method is used to enable/disable specific buttons, based on OS, etc. # # You want to use the Combobox option of state='disabled'. # # There are three options for state as follows: # # state='normal' which is the fully functional Combobox. # state='readonly' which is the Combobox with a value, but can't be changed (directly). # state='disabled' which is where the Combobox cannot be interacted with. # # self.highlight_button = ttk.Style() # self.highlight_button.configure('Highlight.TButton', foreground='red') # ttk.Button(self.mainframe, text="Query this system", style='Highlight.TButton', command= lambda: self.query_jamf_me()).grid(column=2, row=20, padx =3, sticky=W) self.logger.info("%s: activated" % inspect.stack()[0][3]) if self.platform == "Mac": self.jamf_management_btn.configure(state="normal") else: self.jamf_management_btn.configure(state="disabled")
def selection(): global master; categorynum = {"Random":9,"Books":10,"Film":11,"Music":12,"Musicals & Theatres":13,"Television":14,"Video Games":15,"Board Games":16, "Science & Nature":17,"Computers":18,"Mathematics":19,"Mythology":20,"Sports":21,"Geography":22,"History":23,"Politics":24,"Art":25, "Celebrities":26,"Animals":27,"Vehicles":28,"Comics":29,"Gadgets":30,"Japanese Anime & Manga":31,"Cartoon & Animations":32} master = methods.define_window("Pick your choice","400x300") category_var = StringVar(master) category_var.set("Random") #default value category = ttk.Combobox(master, state = "readonly", textvariable = category_var, values = ["Random","Books","Film","Music","Musicals & Theatres","Television","Video Games","Board Games", "Science & Nature","Computers","Mathematics","Mythology","Sports","Geography","History","Politics","Art", "Celebrities","Animals","Vehicles","Comics","Gadgets","Japanese Anime & Manga","Cartoon & Animations"]) category.pack() category.place(relx=.5,rely=.4, anchor="center") number_var = StringVar(master) number_var.set("5") #default value num = ttk.Combobox(master, state = "readonly", textvariable = number_var, values=["5","10","15","20"]) num.pack() num.place(relx=.5,rely=.5,anchor="center") def getinput(): category = category_var.get() if category == "Random": category = random.randint(9,32) else: category = categorynum[category] number = number_var.get() master.destroy(); quizUI(category, number) def back(): master.destroy() profile.show_window() Button(master, text = "Back", command = back).pack(side=BOTTOM, pady = 10) Button(master, text = "Play!", command = getinput).pack(side=BOTTOM, pady=50) master.mainloop()
def add_graph_controls_frame(self): graphControls = Tk.Frame(self.root) graphControls.grid(row=11) #Title Tk.Label(graphControls, text = 'Title').grid(row = 1, column = 0) self.ctlTitle = Tk.Entry(graphControls) self.ctlTitle.config(textvariable = self.Title) self.ctlTitle.grid(row = 1, column = 1, columnspan = 10, sticky = 'we', pady=10) #YMin Selector Tk.Label(graphControls, text = 'Min Y' ).grid(row =2, column=1, padx=3) self.yMinSelector = Tk.Entry(graphControls) self.yMinSelector.config(textvariable = str(self.yMin)) self.yMinSelector.config(width=5) self.yMinSelector.grid(row = 2, column = 2, sticky = 'w') #YMax Selector Tk.Label(graphControls, text = 'Max Y' ).grid(row = 2, column=3) self.yMaxSelector = Tk.Entry(graphControls) self.yMaxSelector.config(textvariable = str(self.yMax)) self.yMaxSelector.config(width=5) self.yMaxSelector.grid(row = 2, column = 4, sticky = 'w') #XMin Selector Tk.Label(graphControls, text = 'Min X' ).grid(row =3, column=1, padx=3) self.xMinSelector = Tk.Entry(graphControls) self.xMinSelector.config(textvariable = str(self.xMin)) self.xMinSelector.config(width=5) self.xMinSelector.grid(row = 3, column = 2, sticky = 'w') #XMax Selector Tk.Label(graphControls, text = 'Max X' ).grid(row = 3, column=3) self.xMaxSelector = Tk.Entry(graphControls) self.xMaxSelector.config(textvariable = str(self.xMax)) self.xMaxSelector.config(width=5) self.xMaxSelector.grid(row = 3, column = 4, sticky = 'w') #Graph Type combobox control Tk.Label(graphControls, text = 'Graph Type' ).grid(row = 4, column=1) self.ctlPlotType = ttk.Combobox(graphControls, values = ('line','bar')) self.ctlPlotType.set(self.plotType) self.ctlPlotType.grid(row = 4, column = 2) #X Label Tk.Label(graphControls, text = 'X-Axis Label' ).grid(row = 5, column=1) self.ctlXLabel = Tk.Entry(graphControls) self.ctlXLabel.config(textvariable = str(self.xLabel)) self.ctlXLabel.grid(row = 5, column = 2, columnspan=3, sticky = 'we') #Y Label Tk.Label(graphControls, text = 'Y-Axis Label' ).grid(row = 6, column=1) self.ctlYLabel = Tk.Entry(graphControls) self.ctlYLabel.config(textvariable = str(self.yLabel)) self.ctlYLabel.grid(row = 6, column = 2, columnspan=3, sticky = 'we') #Draw Graph Button self.btnDrawGraph = Tk.Button(graphControls, text = 'Re-Draw Graph') self.btnDrawGraph.grid(row = 10, column = 4, sticky = 'e', pady=10) self.btnDrawGraph.bind("<Button 1>", self.redrawGraph)
def addActionFrame(self): frame=Frame(self,relief=tk.RAISED,borderwidth=1) frame.pack(fill=tk.X,side=tk.TOP,\ expand=0,padx=8,pady=5) #label=tk.Label(frame,text='Actions:',bg='#bbb') #label.grid(row=0,column=0,sticky=tk.W,padx=8) #---------------Action checkbuttons--------------- frame.columnconfigure(0,weight=1) #---------------------2nd row--------------------- subframe=Frame(frame) subframe.grid(row=1,column=0,columnspan=6,sticky=tk.W+tk.E,\ pady=5) #-------------------Album options------------------- albumlabel=tk.Label(subframe,text=dgbk('?:'),\ bg='#bbb') albumlabel.pack(side=tk.LEFT, padx=8) self.album=tk.StringVar() self.albumnames=['All',] self.albummenu=Combobox(subframe,textvariable=\ self.album,values=self.albumnames,state='readonly') self.albummenu.current(0) self.albummenu.bind('<<ComboboxSelected>>',self.setAlbum) self.albummenu.pack(side=tk.LEFT,padx=8) #-------------------Quit button------------------- quit_button=tk.Button(subframe,text=dgbk('?'),\ command=self.quit) quit_button.pack(side=tk.RIGHT,padx=8) #-------------------Stop button------------------- ''' self.stop_button=tk.Button(subframe,text='Stop',\ command=self.stop) self.stop_button.pack(side=tk.RIGHT,padx=8) ''' #-------------------Start button------------------- self.start_button=tk.Button(subframe,text=dgbk('?'),\ command=self.start,state=tk.DISABLED) self.start_button.pack(side=tk.RIGHT,pady=8) #-------------------Help button------------------- self.help_button=tk.Button(subframe,text=dgbk(''),\ command=self.showHelp) self.help_button.pack(side=tk.RIGHT,padx=8)
def test_values(self): def check_get_current(getval, currval): self.assertEqual(self.combo.get(), getval) self.assertEqual(self.combo.current(), currval) self.assertEqual(self.combo['values'], () if tcl_version < (8, 5) else '') check_get_current('', -1) self.checkParam(self.combo, 'values', 'mon tue wed thur', expected=('mon', 'tue', 'wed', 'thur')) self.checkParam(self.combo, 'values', ('mon', 'tue', 'wed', 'thur')) self.checkParam(self.combo, 'values', (42, 3.14, '', 'any string')) self.checkParam(self.combo, 'values', () if tcl_version < (8, 5) else '') self.combo['values'] = ['a', 1, 'c'] self.combo.set('c') check_get_current('c', 2) self.combo.current(0) check_get_current('a', 0) self.combo.set('d') check_get_current('d', -1) # testing values with empty string self.combo.set('') self.combo['values'] = (1, 2, '', 3) check_get_current('', 2) # testing values with empty string set through configure self.combo.configure(values=[1, '', 2]) self.assertEqual(self.combo['values'], ('1', '', '2') if self.wantobjects else '1 {} 2') # testing values with spaces self.combo['values'] = ['a b', 'a\tb', 'a\nb'] self.assertEqual(self.combo['values'], ('a b', 'a\tb', 'a\nb') if self.wantobjects else '{a b} {a\tb} {a\nb}') # testing values with special characters self.combo['values'] = [r'a\tb', '"a"', '} {'] self.assertEqual(self.combo['values'], (r'a\tb', '"a"', '} {') if self.wantobjects else r'a\\tb {"a"} \}\ \{') # out of range self.assertRaises(tkinter.TclError, self.combo.current, len(self.combo['values'])) # it expects an integer (or something that can be converted to int) self.assertRaises(tkinter.TclError, self.combo.current, '') # testing creating combobox with empty string in values combo2 = ttk.Combobox(self.root, values=[1, 2, '']) self.assertEqual(combo2['values'], ('1', '2', '') if self.wantobjects else '1 2 {}') combo2.destroy()
def __selector (self, position) : self.selectorVal = Tkinter.StringVar() self.selectorVal.set("HD") videoType = ['HD', '??', '??'] s = ttk.Combobox(position, width = 5, textvariable = self.selectorVal, state='readonly', values = videoType) return s
def test_values(self): def check_get_current(getval, currval): self.assertEqual(self.combo.get(), getval) self.assertEqual(self.combo.current(), currval) check_get_current('', -1) self.combo['values'] = ['a', 1, 'c'] self.combo.set('c') check_get_current('c', 2) self.combo.current(0) check_get_current('a', 0) self.combo.set('d') check_get_current('d', -1) # testing values with empty string self.combo.set('') self.combo['values'] = (1, 2, '', 3) check_get_current('', 2) # testing values with empty string set through configure self.combo.configure(values=[1, '', 2]) self.assertEqual(self.combo['values'], ('1', '', '2')) # testing values with spaces self.combo['values'] = ['a b', 'a\tb', 'a\nb'] self.assertEqual(self.combo['values'], ('a b', 'a\tb', 'a\nb')) # testing values with special characters self.combo['values'] = [r'a\tb', '"a"', '} {'] self.assertEqual(self.combo['values'], (r'a\tb', '"a"', '} {')) # out of range self.assertRaises(Tkinter.TclError, self.combo.current, len(self.combo['values'])) # it expects an integer (or something that can be converted to int) self.assertRaises(Tkinter.TclError, self.combo.current, '') # testing creating combobox with empty string in values combo2 = ttk.Combobox(values=[1, 2, '']) self.assertEqual(combo2['values'], ('1', '2', '')) combo2.destroy()
def __init__(self, parent): ttk.Frame.__init__(self, parent) self.parent = parent self['padding'] = '4' self.TKvariables = {} #Read in the defaults for key in defaults: if key[0:5] == 'graph' or key.find('ylims') >= 0: self.TKvariables.update({key:[]}) for val in range(len(defaults[key])): self.TKvariables[key].append(tk.StringVar(value=defaults[key][val])) else: self.TKvariables.update({key:tk.StringVar(value=defaults[key])}) num_vars = int(self.TKvariables['datalength'].get()) self.datalist = list(range(1,num_vars+1)) #Create a combobox containing the available COM ports comlst = self.get_comlst() self.COMbox = ttk.Labelframe(self, text='COM port to source data from') self.COMcombo = ttk.Combobox(self.COMbox, width=60, values=comlst, \ state='readonly', textvariable=self.TKvariables['COMport'],\ postcommand=self.updateCOMbox ) self.COMbox.grid(row = 0, column = 0, columnspan = 5) self.COMcombo.grid() #Create an "about" text box ABOUTframe = ttk.LabelFrame(self, text = 'What it does') ABOUTlabel = ttk.Label(ABOUTframe, text= \ 'Graphs data coming in over the serial port in a comma ' 'seperated variable string. Hover over each option to get ' 'a description of what the setting does', wraplength = 140) ABOUTframe.grid(row=1, column = 0, rowspan = 2, columnspan = 2, \ sticky = 'nw, se', padx= 3, pady = 5) CreateToolTip(ABOUTlabel,\ "The default values can be changed by opening defaults.py with a text " "editor and changing the values") ABOUTlabel.pack() #Create a Graph! and About buttons GObut = ttk.Button(self, text='Go!', command=self.goButton) GObut.grid(row=6, column = 0, sticky = 'we') ABOUTbut = ttk.Button(self, text='About', command=self.aboutButton) ABOUTbut.grid(row = 6, column = 1, sticky = 'we') #Create an instance of the class for the config panel notebook = ConfigNotebook(self, self) #Update the state of the graphs based on the defaults and grid notebook.updateGraphs() notebook.grid(row=1, column=3, columnspan=2, rowspan=6, sticky = 'nsew', \ padx = 5, pady = 5) #Bind the enter key to start the program self.parent.bind("<Return>", lambda event:self.goButton())