简体   繁体   中英

Tkinter grid doesn't expand when the windows change its size

I'm new using Tkinter, I already have this problem, I was thinking the problem was the column and row configure, but that doesn't change anything. This is what I get:

在此处输入图片说明

This is my code:

class Application(Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.master = master
        self.grid()
        self.grid_rowconfigure(0, weight=1)
        self.grid_rowconfigure(1, weight=1)
        self.grid_rowconfigure(2, weight=1)
        self.grid_rowconfigure(3, weight=1)
        self.grid_rowconfigure(4, weight=1)
        self.grid_columnconfigure(0, weight=1)
        self.grid_columnconfigure(1, weight=1)
        self.create_widgets()

    def create_widgets(self):
        self.label = Label(self, justify=LEFT)
        self.label["text"] = "Número:"
        self.label.grid(column=0, row=0, sticky="e")

        self.input = Entry(self)
        self.input.grid(column=1, row=0, sticky="we")

        self.baseValue = StringVar()
        self.baseValue.set(basesNames[0]) # default value

        self.label2 = Label(self)
        self.label2["text"] = "Base:"
        self.label2.grid(column=0, row=1, sticky="e")

        self.menuNumber = ttk.Combobox(self, textvariable=self.baseValue)
        self.menuNumber['values'] = basesNames
        self.menuNumber.grid(column=1, row=1)

        self.label2 = Label(self)
        self.label2["text"] = "Base de resultado: "
        self.label2.grid(column=0, row=2, sticky="e")

        self.baseValueResult = StringVar()
        self.baseValueResult.set(basesNames[0]) # default value

        self.menuNumberResult = ttk.Combobox(self, textvariable=self.baseValueResult)
        self.menuNumberResult['values'] = basesNames
        self.menuNumberResult.grid(column=1, row=2)

        self.convert = ttk.Button(self, text="Convertir", command=self.convertNumber)
        self.convert.grid(row = 4, columnspan=2, sticky="sn")

        self.result = Label(self)
        self.result["text"] = "Your result is: "
        self.result.grid(row=3, columnspan=2, sticky="ew")

root = Tk()
app = Application(master=root)
app.master.title("Conversor de bases")
app.master.maxsize(1000, 400)
app.mainloop()

Also, I checked if the sticky param was necessary but that doesn't change neither

Your widgets are wrapped inside the frame Application . So you need to make the frame expands as well.

from tkinter import *
from tkinter import ttk

basesNames = ["a","b","c","d","e"]

class Application(Frame):
    def __init__(self, master=None):
        ...

root = Tk()
app = Application(master=root)
app.master.title("Conversor de bases")
app.master.maxsize(1000, 400)
root.rowconfigure(0,weight=1)
root.columnconfigure(0,weight=1)
root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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