2017-03-21 126 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

其他一些有用的链接:

相关问题