Python 3.6 - 根据框架大小调整Tkinter按钮的大小

4

我一直在stackoverflow上寻找答案,但是我始终无法让任何东西起作用,所以我现在在这里提问。我有一个小程序,有三个按钮和一个标签,它们都在一个网格中。我想知道如何使按钮和标签与窗口大小或形状无关,就像调整图像大小时,所有内容都保持相同的大小。

以下是我的代码:

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
我不太确定你在问什么。你是说如果你调整窗口大小变大,你希望所有的东西都会跟着放大吗? - Bryan Oakley
@BryanOakley 是的,没错 - Michael Duffett
1个回答

4

这里有一个关于网格布局的好教程。只需滚动到“处理调整大小”部分,您就会注意到如何使用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()

使用哪种方法完全取决于您自己的选择!关于调整大小、网格和排列等方面有一个很好的主题讨论!请看这里 其他一些有用的链接:

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接