我一直在寻找堆栈溢出多年来试图找到答案,但我无法得到任何工作,因此我问这个问题。我有一个带有三个按钮和一个标签的小程序,它们都在网格中。我想知道无论尺寸或形状如何,按钮和标签相对于框架都保持不变。与我调整图像大小相似,所有内容都保持不变。
这是我的代码:
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()
答案 0 :(得分:3)
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=NSEW
和columnconfigure
/ rowconfigure
,看起来像您希望的那样!
这方面的缺点是需要配置每个容器!
但是,在pack
经理中,有更直观且执行相同的角色选项 - fill
和expand
!
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
其他一些有用的链接: