Python 3.6 - Resizing Tkinter Buttons In Accordance With Frame Size

守給你的承諾、 提交于 2020-12-31 01:22:30

问题


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.

Here is my code:

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()  

Sorry if the answer is obvious, I have already tried pack()


回答1:


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.

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()

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!

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

Some other useful links:

  • Tkinter Geometry Managers
  • Grid Geometry Manager
  • grid columnconfigure
  • grid rowconfigure


来源:https://stackoverflow.com/questions/42938090/python-3-6-resizing-tkinter-buttons-in-accordance-with-frame-size

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!