2017-04-26 55 views
0

我有一個問題,我有一個滾動框顯示幾行,每行約200個字符寬。我有寬度設置爲125這是不夠的。但是,當我撞過它大約175我的滾動條消失。如果我將它的值設置爲100或更低,它將滾動瀏覽所有的數據,但這是一個非常小的窗口。我希望窗口成爲框架的大小並滾動瀏覽所有代碼。 代碼:Tkinter Listbox寬度干擾滾動條


import Tkinter as tk 
import tkFont 
def view(): 
    data = ['|unique_id |  id |    species | sex_age | collector |       location |     preparator | collection_date | entered_date | innitials |   notes', 
      '|  88 | A-1444 |  puffinus grseus |  n/a |  n/a |        n/a |       n/a |  13 May 2013 | 27 Apr 2017 |  EB | TL: 395mm WC:', 
      '|  72 | A-1444 |  puffinus grseus |  n/a |  n/a |        n/a |       n/a |  13 May 2013 | 27 Apr 2017 |  EB | TL: 395mm WC:', 
      '|  71 | A-1445 |  anas clypeata |  M | G. Webber |        n/a |      A. Zack |  23 Oct 2013 | 26 Apr 2017 |  EB | TL: 395mm WC:', 
      '|  87 | A-1445 |  anas clypeata |  M | G. Webber |        n/a |      A. Zack |  23 Oct 2013 | 26 Apr 2017 |  EB | TL: 395mm WC:'] 
    size = [800, 600] 
    obj_main_frame = tk.Tk() 
    data_frame = tk.Frame(obj_main_frame, width=size[0], height=size[1]) 
    scrollbar_y = tk.Scrollbar(obj_main_frame, orient=tk.VERTICAL) 
    scrollbar_x = tk.Scrollbar(obj_main_frame, orient=tk.HORIZONTAL) 
    data_scrollable = tk.Listbox(data_frame, font=tkFont.Font(family="Courier", size=10), selectbackground="gray", selectmode=tk.SINGLE, width=150, yscrollcommand=scrollbar_y.set, xscrollcommand=scrollbar_x.set) 
    scrollbar_y.config(command=data_scrollable.yview) 
    scrollbar_x.config(command=data_scrollable.xview) 
    obj_main_frame.grid() 
    data_frame.grid_propagate(0) 
    data_frame.grid(row=0, column=0, sticky='nsew') 
    scrollbar_x.grid(row=1, column=0, sticky='ew') 
    scrollbar_y.grid(row=0, column=1, sticky='ns') 
    data_scrollable.grid(row=0, column=0, sticky='nsew') 
    for i, datum in enumerate(data): 
     data_scrollable.insert(tk.END, datum) 

    obj_main_frame.mainloop() 
    return 

if __name__ == '__main__': 
    view() 

回答

1

你在你對準了2次失誤。

  • 首先,你要確定你的列表框中的刪除width參數,所以它可以調整到其父的大小:

    data_scrollable = tk.Listbox(data_frame, 
              font=tkFont.Font(family="Courier", size=10), 
              selectbackground="gray", selectmode=tk.SINGLE, 
              yscrollcommand=scrollbar_y.set, 
              xscrollcommand=scrollbar_x.set) 
    
  • 其次,在其框架對齊列表框後,你必須到相應的行和列給非零的權重,以允許列表框擴展:

    data_scrollable.grid(row=0, column=0, sticky='nsew') 
    data_frame.columnconfigure(0, weight=1) 
    data_frame.rowconfigure(0, weight=1) 
    

參見:Tkinter Grid Manager documentation