使用tkinter / grid将自动滚动的Treeview放入框架中

时间:2018-06-04 18:44:18

标签: python tkinter treeview autoscroll

我绝望地尝试使用网格将自动滚动Treeview放在框架中。无论我做什么,滚动条都不会出现。另一方面,如果我使用的地方,它的工作原理。但是由于我的表格中的所有内容都是使用网格,所以我希望保持这种状态。

所以,这段代码有效:

try:
   from Tkinter import *
except ImportError:
   from tkinter import *
try:
   import ttk
   py3 = False
except ImportError:
   import tkinter.ttk as ttk
   py3 = True


class OperationWindow(Frame):
   def __init__(self, master):
       Frame.__init__(self, master)
       self.master = master
       self.frTable = Frame(master, width=920, height=250)
       self.frTable.grid(row=0, column=0)
       self.frTable.grid_propagate(0)

       self.Table = ScrolledTreeView(self.frTable)
       self.Table.place(relx=0.0, rely=0.0, relheight=0.96, 
          relwidth=1.0)
       self.Table['columns'] = g_list_col_names

class AutoScroll(object):
   def __init__(self, master):
       try:
           vsb = ttk.Scrollbar(master, orient='vertical', command=self.yview)
       except:
           pass
       hsb = ttk.Scrollbar(master, orient='horizontal', command=self.xview)

       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)

       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))

   @staticmethod
   def _autoscroll(sbar):
       def wrapped(first, last):
           first, last = float(first), float(last)
           if first <= 0 and last >= 1:
               sbar.grid_remove()
           else:
               sbar.grid()
           sbar.set(first, last)
       return wrapped

   def __str__(self):
       return str(self.master)


def _create_container(func):
   def wrapped(cls, master, **kw):
       container = ttk.Frame(master)
       return func(cls, container, **kw)
   return wrapped


class ScrolledTreeView(AutoScroll, ttk.Treeview):
   @_create_container
   def __init__(self, master, **kw):
       ttk.Treeview.__init__(self, master, **kw)
       AutoScroll.__init__(self, master)


def main():
   global g_list_col_names
   g_list_col_names = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 
    '10','11', '12', '13', '14', '15']
   root = Tk()
   master = OperationWindow(root)
   root.mainloop()
main()

如果我改变这一行就停止了:

self.Table.place(relx=0.0, rely=0.0, relheight=0.96, relwidth=1.0)

到此:

self.Table.grid(sticky='ew')

0 个答案:

没有答案