繁体   English   中英

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

[英]Python 3.6 - Resizing Tkinter Buttons In Accordance With Frame 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()

网格打包器有一个很好的教程 只需滚动“处理调整大小”,您就会注意到如何使用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()

您可以选择使用什么! 有一个关于调整大小,网格和包装的好主题! 看一看

其他一些有用的链接:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM