Lucas Cruz
Lucas Cruz

Reputation: 19

How change the number of columns Tkinter Python

I'm creating a program with Tkinter, It have 6 entry labels. I'm with difficulty to grid these labels in the center of page. I don't wanna to use .pack

How can I set the number of columns that the grid has? It seems tkinter ignore when I set column=6 for example.

here my code:

               ##Input 1 / Input de parametros
            self.PwNomeLabel1 = Label(text = "Cliente:")
            self.PwNomeLabel1["font"] = ("10")
            self.PwNomeLabel1.grid(row=0,column=2,sticky=W)
            self.inputpwtest1 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest1["width"] = 30
            self.inputpwtest1.grid(row=0, column=3)
            ##Input 2
            self.PwNomeLabel2 = Label(text = "Responsavel por Teste:")
            self.PwNomeLabel2["font"] = ("10")
            self.PwNomeLabel2.grid(row=1,column=2,sticky=W)
            self.inputpwtest2 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest2["width"] = 30
            self.inputpwtest2.grid(row=1, column=3)
            ##Input 3
            self.PwNomeLabel3 = Label(text = "Nome do Sistema:")
            self.PwNomeLabel3["font"] = ("10")
            self.PwNomeLabel3.grid(row=2,column=2,sticky=W)
            self.inputpwtest3 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest3["width"] = 30
            self.inputpwtest3.grid(row=2,column=3)
            ##Input 4
            self.PwNomeLabel4 = Label(text = "Ref:")
            self.PwNomeLabel4["font"] = ("10")
            self.PwNomeLabel4.grid(row=3,column=2,sticky=W)
            self.inputpwtest4 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest4["width"] = 30
            self.inputpwtest4.grid(row=3,column=3)
            ##Input 5
            self.PwNomeLabel5 = Label(text = "Data Base:")
            self.PwNomeLabel5["font"] = ("10")
            self.PwNomeLabel5.grid(row=4,column=2,sticky=W)
            self.inputpwtest5 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest5["width"] = 30
            self.inputpwtest5.grid(row=4,column=3)
            ##Input 6
            self.PwNomeLabel6 = Label(text = "Data Teste:")
            self.PwNomeLabel6["font"] = ("10")
            self.PwNomeLabel6.grid(row=5,column=2,sticky=W)
            self.inputpwtest6 = Entry(borderwidth= 2, validate='key')
            self.inputpwtest6["width"] = 30
            self.inputpwtest6.grid(row=5,column=3)

            root = Tk()
            root.title("TEST PLATFORM")
            Application(root)
            root.geometry('1366x768')
            root.mainloop()

In this way its results:

enter image description here

If I change label columns to "4" and "5" for the entry results:

enter image description here

Like the prints, It seems the Tkinter get confused and the grid stay desorganized

Upvotes: 0

Views: 2253

Answers (2)

Mike - SMT
Mike - SMT

Reputation: 15226

As mentioned in the comments you are likely needing to manage row/column weights.

Here is an example that does that while also showing how you can reduce your code by dynamically generating labels and entry fields instead of writing a wall of text for all of them.

import tkinter as tk


root = tk.Tk()
root.geometry('800x500')
root.title("TEST PLATFORM")
label_list = ['Cliente:', 'Responsavel por Teste:', 'Nome do Sistema:', 'Ref:', 'Data Base:', 'Data Teste:']
entry_list = []

# Row and Column configure to manage weights
root.columnconfigure(0, weight=1)
root.columnconfigure(2, weight=1)
root.rowconfigure(0, weight=1)
root.rowconfigure(2, weight=1)

# Add a frame to hold the rest of the widgets and place that frame in the row/column without a weight.
# This will allow us to center everything that we place in the frame.
frame = tk.Frame(root)
frame.grid(row=1, column=1)

# use a loop to create our widgets.
for ndex, label in enumerate(label_list):
    tk.Label(frame, text=label, font='10').grid(row=ndex, column=0, sticky='w')
    # Store the entry widgets in a list for later use
    entry_list.append(tk.Entry(frame, borderwidth=2, width=30))
    entry_list[-1].grid(row=ndex, column=1)

# Get and print each entry value.
def print_entries():
    for entry in entry_list:
        print(entry.get())


tk.Button(frame, text='Selecionar Arquivo', command=print_entries).grid(row=len(label_list)+1, column=0, columnspan=2)
root.mainloop()

Upvotes: 1

Phoenixo
Phoenixo

Reputation: 2113

If a column is empty (no widget inside), then it will not appear.

In the following example, I put a frame in column 0 with width=100 in order to have my column 1 "centered". I also have the parameter weight=1 for the 3 columns, so it grows the same way when I expand the window.

import tkinter as tk

root = tk.Tk()
for i in range(3):
    root.columnconfigure(i, weight=1)

tk.Frame(root, width=100).grid(row=0, column=0, sticky='w')
# tk.Label(root, text='Left').grid(row=0, column=0, sticky='w')
tk.Label(root, text='Center').grid(row=0, column=1)
tk.Label(root, text='Right').grid(row=0, column=2, sticky='e')

root.mainloop()

Upvotes: 0

Related Questions