2017-03-21 162 views
3

我一直在環顧堆棧溢出的年齡試圖找到答案,但我不能得到任何東西的工作,因此我問這個問題。我有一個帶有三個按鈕和一個標籤的小程序,它們在一個網格中。我想知道如何不管大小或形狀的按鈕和標籤將保持相對於框架相同。類似於如果我調整圖像大小,一切都保持相同的大小。Python 3.6 - 根據幀大小調整Tkinter按鈕的大小

這裏是我的代碼:

如果
from tkinter import * 

class Window(Frame): #All the stuff for the GUI 
    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.master = master 
     self.init_window() 
     self.grid() 

    def init_window(self): 
     self.master.title("EncryptDecrypt") 
     self.pack(fill = BOTH, expand = 1) 

     quitButton = Button(self, text = "Quit", command = self.client_exit, width = 10, height = 5) #Quit Button 
     quitButton.grid(row = 0, column = 0, sticky = W) 

     encryptModeButton = Button(self, text = "Encrypt", command = lambda: self.execute("decrypted.txt", "encrypted.txt", 1, 0), width = 10, height = 5) #Encrypt Button 
     encryptModeButton.grid(row = 0, column = 1, sticky = W) 

     decryptModeButton = Button(self, text = "Decrypt", command = lambda: self.execute("encrypted.txt", "decrypted.txt", 0, 1), width = 10, height = 5) #Decrypt button 
     decryptModeButton.grid(row = 0, column = 2, sticky = W) 

     myLabel = Label(self, text = "Select The Action You Wish To Undertake", font = ("Purisa", 15)) 
     myLabel.grid(row = 0, column = 3) 
root = Tk() 
root.geometry("610x80") 

app = Window(root) 
root.mainloop() 

對不起,答案是顯而易見的,我已經嘗試pack()

+1

我不太清楚你的要求。你是否在說,如果你調整窗戶的大小,你希望一切都會增長? –

+0

@BryanOakley是的,這是正確的 –

回答

2

There's a good tutorial to grid packer。只需滾動「處理調整大小」,您會注意到如何使用sticky選項並配置列/行對的weight

所以讓我們嘗試用grid打包機的例子:

from tkinter import * 

class Window(Frame): #All the stuff for the GUI 
    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.master = master 
     self.master.minsize(width=650, height=80) 
     self.configure(relief=RAISED, borderwidth=10) 
     self.init_window() 
     self.grid(sticky = NSEW) 

    def init_window(self): 
     self.master.title("EncryptDecrypt") 
     # configure weights; note: that action need for each container! 
     self.master.rowconfigure(0, weight=1) 
     self.master.columnconfigure(0, weight=1) 
     self.rowconfigure(0, weight=1) 
     for i in range(4): 
     self.columnconfigure(i, weight=1) 

     quitButton = Button(self, text = "Quit", width = 10, height = 5) #Quit Button 
     quitButton.grid(row = 0, column = 0, sticky = NSEW) 

     encryptModeButton = Button(self, text = "Encrypt", width = 10, height = 5) #Encrypt Button 
     encryptModeButton.grid(row = 0, column = 1, sticky = NSEW) 

     decryptModeButton = Button(self, text = "Decrypt", width = 10, height = 5) #Decrypt button 
     decryptModeButton.grid(row = 0, column = 2, sticky = NSEW) 

     myLabel = Label(self, text = "Select The Action You Wish To Undertake", font = ("Purisa", 15)) 
     myLabel.grid(row = 0, column = 3, sticky = NSEW) 



root = Tk() 
root.geometry("650x80") 

app = Window(root) 
root.mainloop() 

正如你看到 - 我只是增加了一個sticky=NSEWcolumnconfigure/rowconfigure和看起來它就像你想! 這個弱點是需要配置每個容器!

但在這裏,在pack經理,有更直觀和執行相同的角色選項 - fillexpand

from tkinter import * 

class Window(Frame): #All the stuff for the GUI 
    def __init__(self, master = None): 
     Frame.__init__(self, master) 
     self.master = master 
     self.master.minsize(width=650, height=80) 
     self.configure(relief=RAISED, borderwidth=10) 
     self.init_window() 
     self.pack(fill=BOTH, expand=True) 

    def init_window(self): 
     self.master.title("EncryptDecrypt") 

     quitButton = Button(self, text = "Quit", width = 10, height = 5) #Quit Button 
     quitButton.pack(fill=BOTH, side=LEFT, expand=True) 

     encryptModeButton = Button(self, text = "Encrypt", width = 10, height = 5) #Encrypt Button 
     encryptModeButton.pack(fill=BOTH, side=LEFT, expand=True) 

     decryptModeButton = Button(self, text = "Decrypt", width = 10, height = 5) #Decrypt button 
     decryptModeButton.pack(fill=BOTH, side=LEFT, expand=True) 

     myLabel = Label(self, text = "Select The Action You Wish To Undertake", font = ("Purisa", 15)) 
     myLabel.pack(fill=BOTH, side=LEFT, expand=True) 



root = Tk() 
root.geometry("650x80") 

app = Window(root) 
root.mainloop() 

什麼使用是你的選擇! 還有一個關於調整大小,網格和包的好主題! Take a look

其他一些有用的鏈接:

相關問題