Making A Frame That Can Be Resized in Tkinter

时间:2018-08-06 17:57:51

标签: python python-3.x tkinter frame

I'm using Tkinter with classes, and need to have a frame which can be rezised, without changing the way the class works ( inheriting Frame methods and using a frame as my window ), or the grid manager, I want to do this to get better at classes and grid manager (1). And get better at Tkinter (2). I have tried looking at other stack overflow answers but I can't understand, because there are a lot of other things going on too.

The Code:

from tkinter import *

class Window(Frame): # Inherits Frame methods

    def __init__(self, master):
        Frame.__init__(self, master, bg='LightBlue') # Initializes the frame

        self.master = master # Creates master

        self.init_window()  # Method to grid

        self.master.mainloop() # Runs window

    def init_window(self):

        self.grid()


root = Tk()  # Creates a master
app = Window(root) # Initializes app using root as master

Could someone please help me with this?

EDIT:

Why does Novel's answer work as opposed to...

Code:

from tkinter import *

class Window(Frame): # Inherits Frame methods

    def __init__(self, master):
        Frame.__init__(self, master, bg='LightBlue') # Initializes the frame

        self.master = master # Creates master

        self.init_window()  # Method to grid

        self.master.mainloop() # Runs window

    def init_window(self):

        self.grid(column=0, row=0, sticky=N+S+E+W)

        self.columnconfigure(0, weight=1)

        self.rowconfigure(0, weight=1)

root = Tk()  # Creates a master
app = Window(root) # Initializes app using root as master

Could someone please explain this too?

1 个答案:

答案 0 :(得分:2)

您需要使用columnconfigure和rowconfigure设置网格列和行以随窗口扩展。另外:

  • 请勿使用通配符导入
  • 使用sticky参数使窗口小部件的大小适合网格单元格
  • 请勿在同一框架内布置框架。 IOW不使用self.grid(),始终在外部初始化并使用instance.grid()
  • 在您将tkinter小部件子类化时,一定要通过kwargs
  • 查看更多面向初学者的论坛,例如Learnpython.reddit.com

所有这些:

import tkinter as tk

class Window(tk.Frame): # Inherits Frame methods
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, bg='LightBlue', **kwargs) # Initializes the frame

root = tk.Tk()  # Creates a master
root.geometry('200x200') # set initial size
app = Window(root) # Initializes app using root as master
app.grid(sticky='nsew') # set this widget to keep the size of the grid cell
root.columnconfigure(0, weight=1) # Set the first column to expand with the window
root.rowconfigure(0, weight=1) # Set the first row to expand with the window
root.mainloop() # Runs window

您还可以使用pack以更少的代码完成相同的操作:

import tkinter as tk

class Window(tk.Frame): # Inherits Frame methods
    def __init__(self, master=None, **kwargs):
        tk.Frame.__init__(self, master, bg='LightBlue', **kwargs) # Initializes the frame

root = tk.Tk()  # Creates a master
root.geometry('200x200') # set initial size
app = Window(root) # Initializes app using root as master
app.pack(fill=tk.BOTH, expand=True) # set this widget to fill all available space
root.mainloop() # Runs window

重新编辑:

您需要在Widget的 master 而不是Widget本身上设置rowconfigure。主人决定小部件获得多少空间。因此,您可以根据需要使用self.master.rowconfigure(0,weight = 1),但是我展示的结构对于读取代码要好得多。如果您从子Widget中修改母版,则代码会造成混乱。