Python 3.6 - 根据帧大小调整 Tkinter 按钮的大小
我多年来一直在寻找堆栈溢出的问题,试图找到答案,但我无法解决任何问题,因此我提出了这个问题.我有一个带有三个按钮和一个标签的小程序,它们位于网格中.我想知道按钮和标签的大小或形状如何相对于框架保持不变.类似于如果我调整图像大小,一切都会保持相同的大小.
I have been looking around on stack overflow for ages trying to find the answer to this but I just can't get anything to work, therefore I am asking this question. I have a little program with three buttons and a label, and they are in a grid. I was wondering how no matter the size or shape the buttons and label would stay the same relative to the frame. Similar to if I resized an image, everything stays the same size.
这是我的代码:
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()
Sorry if the answer is obvious, I have already tried pack()
有一个很好的教程网格打包器.只需滚动处理调整大小",您就会注意到如何使用 sticky
选项并配置列/行对的 weight
.
There's a good tutorial to grid packer. Just scroll over "Handling Resize" and you would notice how to use sticky
option and configure weight
of column/row pair.
那么让我们用 grid
打包器试试你的例子:
So let's try your example with grid
packer:
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
并且看起来像您希望的那样工作!缺点是需要配置每个容器!
As you see - I just added a sticky=NSEW
and columnconfigure
/rowconfigure
and seems like it works like you wish!
The weak side of this is the need to configure each container!
但是在这里,在 pack
管理器中,有更直观和执行相同角色的选项 - fill
和 expand
!
But here, in pack
manager, there're more intuitive and performing the same role options - fill
and 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()
用什么是你的选择!还有一个关于调整大小、网格和打包的好话题!看看
What to use is your choice! And there're a good topic about resizing, grid and pack! Take a look
其他一些有用的链接: