Search code examples
pythonsqlitetkintertreeviewttkwidgets

How can I reduce Tree view' s column's width?


I want to reduce the width of the columns so that become fits . as you can see here (https://i.sstatic.net/VK6w8.png) the table is irregular and some columns are not visible. How can I solve this problem?

Here is my code :

def show():


     # connect to database if exist or if doesnt exit create one
     conn= sqlite3.connect("Books_table.bd")


     #create cursors
     Cursor = conn.cursor()

     Cursor.execute("SELECT * , oid FROM books")
     records = Cursor.fetchall()

     Query_window = Toplevel()
     Query_window.title("Show Table ...")
     Frm = Frame(Query_window)
     Frm.pack()

     Table = ttk.Treeview(Frm , column = (1,2,3,4,5,6,7,8) , show = "headings" , height = 5 )
     Table.pack()

     Table.heading(1 , text = "Book name")
     Table.heading(2 , text = "Book number")
     Table.heading(3 , text = "Book writer")
     Table.heading(4 , text = "Book subject")
     Table.heading(5 , text = "Quantity")
     Table.heading(6 , text = "Borrower")
     Table.heading(7 , text = "Return date")
     Table.heading(8 , text = "Availability")


    # insert values to the table 
    for record in records:
        Table.insert("", "end" , value = record )


    #Commit changes
    conn.commit()
    #close database
    conn.close()

Solution

  • You can set the width of a Treeview column with the column() method.

    ...
    Table.heading(1, text="Book name")
    Table.column(1, width=50)
    ...