Pelican
Pelican

Reputation: 303

center a table in tkinter

I am tkintering with tkinter :) and trying to center the "StackQuestion" table.

I do not know what is the option to center horizontally, and or vertically the table inside of the receiving container.

The first class, Class StackQuestion defines how the general windows works and how to switch from container to container. The second, define the table and its content.

I have tried adding different colours to the different containers in order to check which container is active and thereof to try to center everything that is inside the later, without much success.

edit : in order not to have lots of code here I have deleted all the other classes and duplicate "Test" in "for F in (Test, Test):" in order for the code to run on your computer.

table to center

#utf8
#python3
import tkinter as tk
from tkinter import *
from tkinter import ttk


class StackQuestion(tk.Tk):

    def __init__(self, *args, **kwargs):

        tk.Tk.__init__(self, *args, **kwargs)
        container = ttk.Frame(self)

        container.grid()

        #windows resize management
        container.grid_rowconfigure(0, weight=1)
        container.grid_columnconfigure(0, weight=1)

        self.frames = {}
        for F in (Test, Test):
            frame = F(container, self)
            self.frames[F] = frame
            frame.grid(row=1, column=1, sticky='nsew')

        self.show_frame(Test)

    def show_frame(self, cont):

        frame = self.frames[cont]
        frame.tkraise()


class Test(tk.Frame):

    def __init__(self, parent, controller):

        tk.Frame.__init__(self, parent)


        label = tk.Label(self, text = 'StackQuestion')
        label.grid(row=0, column=0, columnspan=2)

        questions = ['Question 1', 'Question 2' , 'Question 3']
        row_number = 1
        column_number = 0

        Tree = ttk.Treeview(self, show ='headings')
        Tree['columns'] = ('Author', 'Subject', 'Date', 'Question')
        Tree.heading('Author', text = 'Author')
        Tree.heading('Subject', text = 'Subject')
        Tree.heading('Date', text = 'Date')
        Tree.heading('Question', text = 'Question')



        for value in questions :
            Tree.insert('', 'end', value, text = value)
            Tree.set(value, 'Author', value)


        Tree.grid()


app = StackQuestion()
app.geometry('1080x700')
app.configure(background='grey')
app.mainloop()

Upvotes: 1

Views: 1083

Answers (1)

John Wilver
John Wilver

Reputation: 38

I'm not an expert python programmer, in fact I'm still a newbie and I'm tkintering too in order to learn. So I've found this "noob-fix" following the hint by toti08

You can change

 container.grid()

with:

container.pack(pady=50)

This will center it horizzontally and give the table a bit of padding top so you can also center it vertically or at least give it a little margin on the border.

Hope this helps until someone more experienced comes along with a solution to your problem :)

Upvotes: 1

Related Questions