2011-09-24 61 views
11

使用Python構建一個小型應用程序供個人使用,並且認爲我會嘗試使用Tkinter進行一些GUI編程。這是我創建至今的GUI:使用Tk Grid幾何體管理器的GUI佈局

GUI layout using Tkinter

疑義:

我怎樣才能確保三個LableFrames - A,B和C的截圖 - 有寬度相同? (或者說,寬度等於三者中最寬的那個?例如,在截圖中,A是最寬的,我希望B和C也寬到D)。

(它不需要動態計算 - 如果我可以確保寬度在我第一次編碼時是相同的,就足夠了,它們不需要在運行時更改。)

Tk的網格幾何經理懷疑

  1. 當您使用框架,是網格特定的(行,列)只對幀的大小或它計算基於所述形式的大小(根窗口)?

  2. 如何在網格中確定列的大小?

  3. 我還沒有完全理解網格中的「重量」。什麼時候應該使用它?

Python的GUI代碼

import Tkinter 

if __name__ == '__main__': 
    form = Tkinter.Tk() 

    getFld = Tkinter.IntVar() 

    form.wm_title('File Parser') 

    stepOne = Tkinter.LabelFrame(form, text=" 1. Enter File Details: ") 
    stepOne.grid(row=0, columnspan=7, sticky='W', \ 
       padx=5, pady=5, ipadx=5, ipady=5) 

    helpLf = Tkinter.LabelFrame(form, text=" Quick Help ") 
    helpLf.grid(row=0, column=9, columnspan=2, rowspan=8, \ 
       sticky='NS', padx=5, pady=5) 
    helpLbl = Tkinter.Label(helpLf, text="Help will come - ask for it.") 
    helpLbl.grid(row=0) 

    stepTwo = Tkinter.LabelFrame(form, text=" 2. Enter Table Details: ") 
    stepTwo.grid(row=2, columnspan=7, sticky='W', \ 
       padx=5, pady=5, ipadx=5, ipady=5) 

    stepThree = Tkinter.LabelFrame(form, text=" 3. Configure: ") 
    stepThree.grid(row=3, columnspan=7, sticky='W', \ 
        padx=5, pady=5, ipadx=5, ipady=5) 

    inFileLbl = Tkinter.Label(stepOne, text="Select the File:") 
    inFileLbl.grid(row=0, column=0, sticky='E', padx=5, pady=2) 

    inFileTxt = Tkinter.Entry(stepOne) 
    inFileTxt.grid(row=0, column=1, columnspan=7, sticky="WE", pady=3) 

    inFileBtn = Tkinter.Button(stepOne, text="Browse ...") 
    inFileBtn.grid(row=0, column=8, sticky='W', padx=5, pady=2) 

    outFileLbl = Tkinter.Label(stepOne, text="Save File to:") 
    outFileLbl.grid(row=1, column=0, sticky='E', padx=5, pady=2) 

    outFileTxt = Tkinter.Entry(stepOne) 
    outFileTxt.grid(row=1, column=1, columnspan=7, sticky="WE", pady=2) 

    outFileBtn = Tkinter.Button(stepOne, text="Browse ...") 
    outFileBtn.grid(row=1, column=8, sticky='W', padx=5, pady=2) 

    inEncLbl = Tkinter.Label(stepOne, text="Input File Encoding:") 
    inEncLbl.grid(row=2, column=0, sticky='E', padx=5, pady=2) 

    inEncTxt = Tkinter.Entry(stepOne) 
    inEncTxt.grid(row=2, column=1, sticky='E', pady=2) 

    outEncLbl = Tkinter.Label(stepOne, text="Output File Encoding:") 
    outEncLbl.grid(row=2, column=5, padx=5, pady=2) 

    outEncTxt = Tkinter.Entry(stepOne) 
    outEncTxt.grid(row=2, column=7, pady=2) 

    outTblLbl = Tkinter.Label(stepTwo, \ 
      text="Enter the name of the table to be used in the statements:") 
    outTblLbl.grid(row=3, column=0, sticky='W', padx=5, pady=2) 

    outTblTxt = Tkinter.Entry(stepTwo) 
    outTblTxt.grid(row=3, column=1, columnspan=3, pady=2, sticky='WE') 

    fldLbl = Tkinter.Label(stepTwo, \ 
          text="Enter the field (column) names of the table:") 
    fldLbl.grid(row=4, column=0, padx=5, pady=2, sticky='W') 

    getFldChk = Tkinter.Checkbutton(stepTwo, \ 
          text="Get fields automatically from input file",\ 
          onvalue=1, offvalue=0) 
    getFldChk.grid(row=4, column=1, columnspan=3, pady=2, sticky='WE') 

    fldRowTxt = Tkinter.Entry(stepTwo) 
    fldRowTxt.grid(row=5, columnspan=5, padx=5, pady=2, sticky='WE') 

    transChk = Tkinter.Checkbutton(stepThree, \ 
       text="Enable Transaction", onvalue=1, offvalue=0) 
    transChk.grid(row=6, sticky='W', padx=5, pady=2) 

    transRwLbl = Tkinter.Label(stepThree, \ 
       text=" => Specify number of rows per transaction:") 
    transRwLbl.grid(row=6, column=2, columnspan=2, \ 
        sticky='W', padx=5, pady=2) 

    transRwTxt = Tkinter.Entry(stepThree) 
    transRwTxt.grid(row=6, column=4, sticky='WE') 

    form.mainloop() 

(注:對於Python 2.4.3)

回答

10

如果使用相同的columnspan和使用粘性= '我們' 在所有三個LabelFrames那麼他們應該有相同的寬度。例如,要使用

stepTwo = Tkinter.LabelFrame(form, text=" 2. Enter Table Details: ") 
stepTwo.grid(row=2, columnspan=7, sticky='WE', \ 
      padx=5, pady=5, ipadx=5, ipady=5) 

問題

1)當你使用框架,是電網(行,列)特定於僅 大小框架的或者是它根據表格的大小計算 (根窗口)?

這裏有一個相互依存關係。表單的首選大小取決於孩子的首選大小和佈局,但孩子的實際大小將取決於表單的實際大小和佈局。佈局是從子項到根目錄確定首選大小,然後當它到達根目錄時,首選大小將用作實際大小(除非被覆蓋)。然後佈局重新分配實際大小。

2)如何在網格中確定列的大小?

列的首選大小是根據該行中所有項目的最小首選寬度確定的。列的實際大小由首選大小加上父窗口小部件額外空間的某個百分比確定。

3)我還沒有完全理解網格內的「重量」。應該使用 嗎?

重量決定了我上面提到的額外空間的百分比。提供給列的額外空間量是column_weight/total_weight。

+0

非常好的答案,沃恩。 –