2016-07-23 26 views
0

這裏是我的問題:
我有一個labelframe其中包含了「 - 側」選項左對齊一些按鈕(我在一個典型的標籤太測試)。斷行的按鈕

這些按鈕是由一個循環生成的,我想爲該標籤添加一個「wraplength」,以便我的按鈕可以跳過一條線,而不是沿着彼此並排走。

環路&按鈕的代碼:

with open('data.txt', 'r') as data: 
    for line in data: 
     line = line.rstrip('\n') 
     status = CheckStatus(line) 
     NewButton = Button(labelframe, state = status, text = line, command = lambda x=line:run(str(x))) 
     NewButton.pack(side = LEFT, padx = 5, pady = 5) 

含有環的框架:

labelframe = LabelFrame(Window, width = 400, height = 150) 
labelframe.pack() 
labelframe.pack_propagate(False) #I Try this to fix the size of the labelframe. 
+1

如果你想在他們一列,只需用'側='top'',他們將棧頂TO-底部。 –

+0

@BryanOakley我知道,但我希望我的按鈕出現在行** S **。換句話說,當標籤達到一定的寬度時,將會創建一個新行。 – TeeVy

+0

哦,所以你想要他們連續,但是當他們碰到你想要他們包裹的邊緣? –

回答

0

好了,所以,最後,我在環路中加入一個增量變量&一個if。當變量等於5時,會創建一個新框架(其中包含按鈕)。不知道這是否是最優化的方法,但我沒有發現任何東西......

def LoadButtons(): 
FrameInfoText.config(justify = CENTER, text = 'Initialization...') 
FrameInfoText.update() 
LineFrame = Frame(labelframe) 
LineFrame.pack() 
ButtonsPerLine = 0 
with open('data.txt', 'r') as data: 
    for line in data: 
     line = line.rstrip('\n') 
     StreamPerLine += 1 
     if ButtonsPerLine > 5: 
      LineFrame = Frame(labelframe) 
      LineFrame.pack() 
      ButtonsPerLine = 1 
     status = CheckStatus(line) 
     NewButton = Button(LineFrame, state = status, text = line, command = lambda x=line:run(str(x))) 
     NewButton.pack(side = LEFT, padx = 5, pady = 5) 
     FrameInfoText.config(text = 'Loading...\n'+line) 
     FrameInfoText.update()