我们从Python开源项目中,提取了以下16个代码示例,用于说明如何使用ttk.Scrollbar()。
def __init__(self, master, on_selected=None, **kwargs): tk.Frame.__init__(self, master, **kwargs) self._cache = {} self.tree = ttk.Treeview(self) self.tree['show'] = 'headings' self.tree['columns'] = ('subject', 'tests', 'status', 'bug') self.tree.heading('subject', text='Subject') self.tree.heading('tests', text='Test') self.tree.heading('status', text='Status') self.tree.heading('bug', text='Bug') self.tree.column('subject', width=60) self.tree.column('tests', width=150) self.tree.column('status', width=40) self.tree.column('bug', width=10) self.tree.bind('<<TreeviewSelect>>', on_selected) ysb = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview) self.tree.grid(row=0, column=0, sticky='nsew') ysb.grid(row=0, column=1, sticky='ns') self.tree.configure(yscroll=ysb.set)
def __init__(self, master, factor = 0.5, **kwargs): self.__scrollableWidgets = [] if 'orient' in kwargs: if kwargs['orient']== tk.VERTICAL: self.__orientLabel = 'y' elif kwargs['orient']== tk.HORIZONTAL: self.__orientLabel = 'x' else: raise Exception("Bad 'orient' argument in scrollbar.") else: self.__orientLabel = 'y' kwargs['command'] = self.onScroll self.factor = factor ttk.Scrollbar.__init__(self, master, **kwargs)
def instructions(self): # msgarea = Text( self.hp_frame, width=140, height=18 ) # msgarea.grid( column=0, row=0, sticky=(W,E) ) msgarea = Text( self.hp_frame, height=18 ) msgarea.pack(side=LEFT, fill=BOTH, expand=1 ) sb = ttk.Scrollbar( self.hp_frame, orient='vertical', command=msgarea.yview ) # sb.grid( row=0, column=1, sticky=(N,S,W) ) sb.pack( side=TOP, fill=BOTH, expand=1 ) msgarea['yscrollcommand'] = sb.set msgarea.bind('<Key>', lambda e: 'break') # ignore all key presses msgarea.insert('end', help.usage) return # ----------------------------------------------------------
def __init__(self, parent): self.parent = parent tk.Frame.__init__(self, self.parent) self.vscroll = ttk.Scrollbar(self, orient=tk.VERTICAL) self.hscroll = ttk.Scrollbar(self, orient=tk.HORIZONTAL) self.console = tk.Text(self, wrap='none', width=60) self.console.config(yscrollcommand = self.vscroll.set) self.console.config(xscrollcommand = self.hscroll.set) self.vscroll.config(command = self.console.yview) self.hscroll.config(command = self.console.xview) self.console.grid(column=0,row=0, sticky=(tk.W, tk.N, tk.S, tk.E)) self.vscroll.grid(column=1, row=0, sticky=(tk.E,tk.N,tk.S)) self.hscroll.grid(column=0,row=1, sticky=(tk.E,tk.W,tk.S)) self.columnconfigure(0, weight=1) self.rowconfigure(0, weight=1) self.console.insert(tk.END, 'Started PIEFACE GUI\n*************************\n') self.console.config(state=tk.DISABLED)
def __init__(self, master, **kwargs): Frame.__init__(self, master) textarea_frame = Frame(self, bd=1, relief=SOLID,**kwargs) textarea_frame.pack(fill=X) textarea_frame.pack_propagate(False) self.textarea = Text(textarea_frame, height=1, pady=self.TAG_ENTRY_PADY, padx=self.TAG_ENTRY_PADX, highlightthickness =0, spacing1=0, spacing2=0, spacing3=0, borderwidth=0, wrap="none") self.textarea.pack(expand=True, fill=BOTH, padx=2) scrollbar = Scrollbar(self, orient=HORIZONTAL, command=self.textarea.xview) scrollbar.pack(fill=X) self.textarea.configure(xscrollcommand=scrollbar.set) self.textarea.bind("<KeyPress>",self._on_keypress) tag = Tag(self.textarea, "") self.textarea.window_create("1.0", window=tag) self.update_idletasks() tag_reqheight = tag.winfo_reqheight() textarea_frame.configure(height=tag_reqheight + 2*self.TAG_ENTRY_PADY+2*self.textarea["borderwidth"]) # I add a hidden frame because I want the cursor centered including when there is no tag self.textarea.window_create("1.0", window=Frame(self.textarea, height=tag_reqheight, width=0, borderwidth=0)) tag.destroy()
def __init__(self, master): # Rozen. Added the try-except clauses so that this class # could be used for scrolled entry widget for which vertical # scrolling is not supported. 5/7/14. try: vsb = ttk.Scrollbar(master, orient='vertical', command=self.yview) except: pass hsb = ttk.Scrollbar(master, orient='horizontal', command=self.xview) #self.configure(yscrollcommand=self._autoscroll(vsb), # xscrollcommand=self._autoscroll(hsb)) try: self.configure(yscrollcommand=self._autoscroll(vsb)) except: pass self.configure(xscrollcommand=self._autoscroll(hsb)) self.grid(column=0, row=0, sticky='nsew') try: vsb.grid(column=1, row=0, sticky='ns') except: pass hsb.grid(column=0, row=1, sticky='ew') master.grid_columnconfigure(0, weight=1) master.grid_rowconfigure(0, weight=1) # Copy geometry methods of master (taken from ScrolledText.py) if py3: methods = Pack.__dict__.keys() | Grid.__dict__.keys() \ | Place.__dict__.keys() else: methods = Pack.__dict__.keys() + Grid.__dict__.keys() \ + Place.__dict__.keys() for meth in methods: if meth[0] != '_' and meth not in ('config', 'configure'): setattr(self, meth, getattr(master, meth))
def create(self, **kwargs): return ttk.Scrollbar(self.root, **kwargs)
def _setup_widgets(self): msg = ttk.Label(wraplength="4i", justify="left", anchor="n", padding=(10, 2, 10, 6), text=("Ttk is the new Tk themed widget set. One of the widgets it " "includes is a tree widget, which can be configured to " "display multiple columns of informational data without " "displaying the tree itself. This is a simple way to build " "a listbox that has multiple columns. Clicking on the " "heading for a column will sort the data by that column. " "You can also change the width of the columns by dragging " "the boundary between them.")) msg.pack(fill='x') container = ttk.Frame() container.pack(fill='both', expand=True) # XXX Sounds like a good support class would be one for constructing # a treeview with scrollbars. self.tree = ttk.Treeview(columns=tree_columns, show="headings") vsb = ttk.Scrollbar(orient="vertical", command=self.tree.yview) hsb = ttk.Scrollbar(orient="horizontal", command=self.tree.xview) self.tree.configure(yscrollcommand=vsb.set, xscrollcommand=hsb.set) self.tree.grid(column=0, row=0, sticky='nsew', in_=container) vsb.grid(column=1, row=0, sticky='ns', in_=container) hsb.grid(column=0, row=1, sticky='ew', in_=container) container.grid_columnconfigure(0, weight=1) container.grid_rowconfigure(0, weight=1)
def __init__(self, root): tk.Frame.__init__(self, root, bg='#10253F') self.packet = None self.label_border = tk.Frame(self, bg='#7F7F7F', height=35) self.label_border.pack_propagate(False) self.label = tk.Label(self.label_border, bg='#a68c7a', fg='#10253F', anchor=tk.W, padx=10, font='TkDefaultFont 12 bold') self.scroll = ttk.Scrollbar(self, style='Vertical.TScrollbar', orient=tk.VERTICAL) ttk.Style().layout('Fields.Treeview', [('Treeview.treearea', {'sticky': 'nswe'})]) ttk.Style().configure('Fields.Treeview', background='#10253F', foreground='#FFFFFF') self.frame = tk.Frame(self, bg='#10253F') self.tree = ttk.Treeview(self.frame, show='tree', yscroll=self.scroll.set, style='Fields.Treeview') self.tree.tag_configure('layer', background='#20354F') self.scroll.config(command=self.tree.yview) self.opened_layers = set() self.tree.bind('<Control-c>', self.copy) # TODO # self.menu = tk.Menu(self) # self.menu.add_command(label='Insert As Filter', command=lambda: root.searchbar.insert(self.packet.getattr()))
def __init__(self, root): tk.Canvas.__init__(self, root, bg='#FEF9F4', highlightthickness=0) ttk.Style().layout('Packets.Treeview', [('Treeview.treearea', {'sticky': 'nswe'})]) ttk.Style().configure('Packets.Treeview', foreground='#000000', background='#FEF9F4') self.packets = {} self.tree_frame = tk.Frame(self, bg='black') self.tree = ttk.Treeview(self.tree_frame, height=0, style='Packets.Treeview', show='headings') self.tree['columns'] = ['no', 'time', 'src', 'dst', 'protocol', 'length'] cdata = [ ('Index', False, 50, tk.E), ('Time', False, 100, tk.CENTER), ('Source', True, 120, tk.W), ('Destination', True, 120, tk.W), ('Protocol', True, 60, tk.CENTER), ('Length', False, 80, tk.E) ] self.size = 0 for i, c in enumerate(cdata): cname = self.tree['columns'][i] self.tree.heading(cname, text=c[0]) self.tree.column(cname, stretch=c[1], width=c[2], anchor=c[3]) for i in range(12): self.tree.tag_configure('n%i' % i, background=colors_normal[i % 2]) self.scroll = ttk.Scrollbar(self, style='Vertical.TScrollbar', orient=tk.VERTICAL, command=self.tree.yview) self.tree.configure(yscrollcommand=self.scroll.set) self.img = tk.PhotoImage(file='CF_back.gif').subsample(2, 2) self.background = self.create_image((350, 160), image=self.img, anchor=tk.CENTER) self.bind('<Configure>', self.recenter_bg) self.rb = False
def open_text_viewer(url, data): text = data.decode(encoding="latin_1") window = Toplevel(top) window.title("Gophersnake text viewer") textview = Text(window, width=80, height=25, wrap="word") textview.insert("1.0", text.replace("\r\n", "\n")) window.bind("<Control-a>", lambda e: select_all()) textview.bind("<Control-c>", lambda e: copy_to_clipboard()) #textview["state"] = "disabled" def select_all(): textview.tag_remove("sel", "1.0", "end") textview.tag_add("sel", "1.0", "end") def copy_to_clipboard(): window.clipboard_clear() window.clipboard_append(textview.get("sel.first", "sel.last")) textview.grid(row=0, column=0, sticky=(N, S, E, W)) textview.focus_set() textscroll = ttk.Scrollbar( window, orient=VERTICAL, command=textview.yview) textview["yscrollcommand"] = textscroll.set textscroll.grid(row=0, column=1, sticky=(N, S)) textstatus = ttk.Label(window, text=url) textstatus.grid(row=1, column=0, sticky=(E, W)) textgrip = ttk.Sizegrip(window) textgrip.grid(row=1, column=1, sticky=(S, E)) window.rowconfigure(0, weight=1) window.columnconfigure(0, weight=1) # Broken as of 2016-08-29
def __init__(self, master, width=None, anchor=N, height=None, mousewheel_speed = 2, scroll_horizontally=True, xscrollbar=None, scroll_vertically=True, yscrollbar=None, outer_background=None, inner_frame=Frame, **kw): Frame.__init__(self, master, class_=self.__class__) if outer_background: self.configure(background=outer_background) self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) self._width = width self._height = height self.canvas = Canvas(self, background=outer_background, highlightthickness=0, width=width, height=height) self.canvas.grid(row=0, column=0, sticky=N+E+W+S) if scroll_vertically: if yscrollbar is not None: self.yscrollbar = yscrollbar else: self.yscrollbar = Scrollbar(self, orient=VERTICAL) self.yscrollbar.grid(row=0, column=1,sticky=N+S) self.canvas.configure(yscrollcommand=self.yscrollbar.set) self.yscrollbar['command']=self.canvas.yview else: self.yscrollbar = None if scroll_horizontally: if xscrollbar is not None: self.xscrollbar = xscrollbar else: self.xscrollbar = Scrollbar(self, orient=HORIZONTAL) self.xscrollbar.grid(row=1, column=0, sticky=E+W) self.canvas.configure(xscrollcommand=self.xscrollbar.set) self.xscrollbar['command']=self.canvas.xview else: self.xscrollbar = None self.rowconfigure(0, weight=1) self.columnconfigure(0, weight=1) self.innerframe = inner_frame(self.canvas, **kw) self.innerframe.pack(anchor=anchor) self.canvas.create_window(0, 0, window=self.innerframe, anchor='nw', tags="inner_frame") self.canvas.bind('<Configure>', self._on_canvas_configure) Mousewheel_Support(self).add_support_to(self.canvas, xscrollbar=self.xscrollbar, yscrollbar=self.yscrollbar)
def __init__(self, master, xml=None, heading_text=None, heading_anchor=None, padding=None, cursor=None, takefocus=None, style=None): Frame.__init__(self, master, class_="XML_Viwer") self._vsb = Scrollbar(self, orient=VERTICAL) self._hsb = Scrollbar(self, orient=HORIZONTAL) kwargs = {} kwargs["yscrollcommand"] = lambda f, l: autoscroll(self._vsb, f, l) kwargs["xscrollcommand"] = lambda f, l: autoscroll(self._hsb, f, l) if style is not None: kwargs["style"] = style if padding is not None: kwargs["padding"] = padding if cursor is not None: kwargs["cursor"] = cursor if takefocus is not None: kwargs["takefocus"] = takefocus self._treeview = Treeview(self, **kwargs) if heading_text is not None: if heading_anchor is not None: self._treeview.heading("#0", text=heading_text, anchor=heading_anchor) else: self._treeview.heading("#0", text=heading_text) self._treeview.bind("<<TreeviewOpen>>", self._on_open) self._treeview.bind("<<TreeviewClose>>", self._on_close) # Without this line, horizontal scrolling doesn't work properly. self._treeview.column("#0", stretch= False) self._vsb['command'] = self._treeview.yview self._hsb['command'] = self._treeview.xview self._treeview.grid(column=0, row=0, sticky=N+S+W+E) self._vsb.grid(column=1, row=0, sticky=N+S) self._hsb.grid(column=0, row=1, sticky=E+W) self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) self._element_tree = None self._item_ID_to_element = {} if xml is not None: self.parse_xml(xml)
def __init__(self, master, width=None, anchor=N, height=None, mousewheel_speed = 2, scroll_horizontally=True, xscrollbar=None, scroll_vertically=True, yscrollbar=None, background=None, inner_frame=Frame, **kw): Frame.__init__(self, master, class_="Scrolling_Area", background=background) self.grid_columnconfigure(0, weight=1) self.grid_rowconfigure(0, weight=1) self._width = width self._height = height self.canvas = Canvas(self, background=background, highlightthickness=0, width=width, height=height) self.canvas.grid(row=0, column=0, sticky=N+E+W+S) if scroll_vertically: if yscrollbar is not None: self.yscrollbar = yscrollbar else: self.yscrollbar = Scrollbar(self, orient=VERTICAL) self.yscrollbar.grid(row=0, column=1,sticky=N+S) self.canvas.configure(yscrollcommand=self.yscrollbar.set) self.yscrollbar['command']=self.canvas.yview else: self.yscrollbar = None if scroll_horizontally: if xscrollbar is not None: self.xscrollbar = xscrollbar else: self.xscrollbar = Scrollbar(self, orient=HORIZONTAL) self.xscrollbar.grid(row=1, column=0, sticky=E+W) self.canvas.configure(xscrollcommand=self.xscrollbar.set) self.xscrollbar['command']=self.canvas.xview else: self.xscrollbar = None self.rowconfigure(0, weight=1) self.columnconfigure(0, weight=1) self.innerframe = inner_frame(self.canvas, **kw) self.innerframe.pack(anchor=anchor) self.canvas.create_window(0, 0, window=self.innerframe, anchor='nw', tags="inner_frame") self.canvas.bind('<Configure>', self._on_canvas_configure) Mousewheel_Support(self).add_support_to(self.canvas, xscrollbar=self.xscrollbar, yscrollbar=self.yscrollbar)
def __init__(self): self.root = Tk() self.root.title("Dependencies") self.menubar = Menu(self.root) self.filemenu = Menu(self.menubar, tearoff=0) self.filemenu.add_command(label="Open", command=self.setRootFolder) self.filemenu.add_separator() self.filemenu.add_command(label="Exit", command=self.root.quit) self.menubar.add_cascade(label="File", menu=self.filemenu) self.root.config(menu=self.menubar) self.mainframe = ttk.Frame(self.root, width=300, height=300) self.root.columnconfigure(0, weight=1) self.root.rowconfigure(0, weight=1) self.mainframe.grid(column=0, row=0, sticky = "NSEW") self.hrulercanvas = Canvas(self.mainframe, width=300, borderwidth=0, background="#ffffff") self.vrulercanvas = Canvas(self.mainframe, height=300, borderwidth=0, background="#ffffff") self.deps_canvas = Canvas(self.mainframe, width=300, height=300, borderwidth=0, background="#ffffff") self.vscrollbar = ttk.Scrollbar(self.mainframe, orient=VERTICAL) self.hscrollbar = ttk.Scrollbar(self.mainframe, orient=HORIZONTAL) self.vscrollbar.config(command=self.yview) self.hscrollbar.config(command=self.xview) self.deps_canvas.config(yscrollcommand=self.vscrollbar.set) self.deps_canvas.config(xscrollcommand=self.hscrollbar.set) self.hrulercanvas.grid(row=0, column=1, sticky="EW") self.vrulercanvas.grid(row=1, column=0, sticky="NS") self.deps_canvas.grid(row=1, column=1, sticky="NSEW") self.vscrollbar.grid(row=1, column=2, sticky="NS") self.hscrollbar.grid(row=2, column=1, sticky="EW") self.hrulerframe = ttk.Frame(self.hrulercanvas) self.vrulerframe = ttk.Frame(self.vrulercanvas) self.hrulerframe.bind("<Configure>", self._onHRulerCanvasConfigure) self.vrulerframe.bind("<Configure>", self._onVRulerCanvasConfigure) self.hrulerwindow = self.hrulercanvas.create_window(0, 0, window=self.hrulerframe, anchor="nw", tags="self.hrulerframe") self.vrulerwindow = self.vrulercanvas.create_window(0, 0, window=self.vrulerframe, anchor="nw", tags="self.vrulerframe") self.deps_frame = ttk.Frame(self.deps_canvas) self.deps_frame.bind("<Configure>", self._onDepsCanvasConfigure) self.deps_window = self.deps_canvas.create_window(0, 0, window=self.deps_frame, anchor="nw", tags="self.deps_frame") self.mainframe.columnconfigure(0, weight=0) self.mainframe.columnconfigure(1, weight=1) self.mainframe.columnconfigure(2, weight=0) self.mainframe.rowconfigure(0, weight=0) self.mainframe.rowconfigure(1, weight=1) self.mainframe.rowconfigure(2, weight=0)