2017-07-12 24 views
3

我正在創建三個「消息小部件」,但它們似乎根據裏面的內容在寬度和高度上進行了調整,是否有可能防止出現這種情況?如何停止調整Tkinter Message小部件?

from tkinter import * 

root = Tk() 
root.configure(background = 'Yellow') 
root.geometry('500x400') 

a = '{}'.format('The Elepthan is Big') 
b = '{}'.format('The Bird') 
c = '{}'.format('The Lion is big and ferocious, kills any animal') 

msg = Message(root, text = a, width=300, justify='left') 
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3) 
msg.pack() 


msg = Message(root, text = b, width=300, justify='left') 
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3) 
msg.pack() 

msg = Message(root, text = c, width=300, justify='left') 
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3) 
msg.pack() 




root.mainloop() 
+0

是其可能的。向我們提供您用於創建消息窗口小部件的代碼示例,並告訴您缺少什麼以防止調整窗口大小。這可能是您需要爲網格添加一些權重。 –

+1

請提供說明您遇到的問題的[mcve]。 –

+0

@BryanOakley完成,我希望所有消息部件具有相同的寬度,但他們根據內容調整大小 - espktro 8分鐘前 – espktro

回答

3

UPDATE:

您有幾種選擇在這裏,但根據您的評論widgets resize according to their content, I want them all with same width我會給,使用pack()幾何管理器最適合您的需要的例子。

用於包裝至少是最簡單的選擇是做pack(fill = BOTH)

做你想做什麼的最快方法是使用框架。該框架將調整爲最大的文本,並且在所有消息窗口小部件上使用pack(fill = BOTH)會將較小的窗口展開爲也是最大窗口小部件大小的框架大小。

from tkinter import * 

root = Tk() 
root.configure(background = 'Yellow') 
root.geometry('500x400') 

a = '{}'.format('The Elepthan is Big') 
b = '{}'.format('The Bird') 
c = '{}'.format('The Lion is big and ferocious, kills any animal') 

frame = Frame(root) 
frame.pack() 

msg = Message(frame, text = a, width=300, justify='left') 
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3) 
msg.pack(fill=BOTH) 


msg = Message(frame, text = b, width=300, justify='left') 
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3) 
msg.pack(fill=BOTH) 

msg = Message(frame, text = c, width=300, justify='left') 
msg.config(bg='lightgreen',relief=RIDGE, font=('times', 9), pady=-2, borderwidth=3) 
msg.pack(fill=BOTH) 

root.mainloop() 
+0

固定寬度而不是父寬度呢?框架= 400消息= 270 ...無論是與網格或包,我只需要具有相同的寬度(270),但不是父窗口小部件(400)的寬度的所有消息窗口小部件。 – espktro

+0

有一些選項。一種是禁用幀的網格大小調整,以便幀被強制保持設定的寬度。我知道還有其他選擇,但我需要考慮一下。我會看到我能找到的。 –

2

您可以找到最長的消息的寬度,然後使用Message widget的padx選項中心所有的人都該長度值的範圍內做到這一點。這裏有一個例子:

from tkinter import * 
import tkinter.font as tkFont 

root = Tk() 
root.configure(background='Yellow') 
root.geometry('500x400') 

a = 'The Elepthan is Big' 
b = 'The Bird' 
c = 'The Lion is big and ferocious, kills any animal' 
messages = a, b, c 

msgfont = tkFont.Font(family='times', size=9) 
maxwidth = max(msgfont.measure(text) for text in messages) # get pixel width of longest one 

for text in messages: 
    msg = Message(root, text=text, width=maxwidth, justify='left') 
    padx = (maxwidth-msgfont.measure(text)) // 2 # padding needed to center text 
    msg.config(bg='lightgreen', relief=RIDGE, font=msgfont, padx=padx, pady=-2, borderwidth=3) 
    msg.pack() 

root.mainloop() 

結果:

enter image description here

+0

僅僅使用一個框架並允許所有東西都可以使用'pack(fill = BOTH)'來調整它的大小並不是一件簡單的事情: –

+0

@Sierra:是的,如果把所有內容都放在一個框架內是可以接受的 - 這取決於整體設計還有什麼事情正在發生。我的答案中顯示的方法可能會涉及更多,但顯示如何控制'Message'小部件而不這樣做(以及如何計算所需的值)。 – martineau

+1

ty的解釋:) –

相關問題