Search code examples
pythontkintertk-toolkittkinter-entryttk

Horizontal scrollbar doesn´t work in a kind of table in Tkinter


I am trying to create a table using tkinter for python:

I have the next code:

from tkinter import *
from  tkinter import ttk

ws  = Tk()
ws.title('PythonGuides')
ws.geometry('500x500')
ws['bg'] = 'Black'

game_frame = Frame(ws)
game_frame.pack()

game_scroll = Scrollbar(game_frame, orient='vertical')
game_scroll.pack(side=RIGHT, fill=Y)

game_scroll = Scrollbar(game_frame,orient='horizontal')
game_scroll.pack(side= BOTTOM,fill=X)

my_game = ttk.Treeview(game_frame,yscrollcommand=game_scroll.set, xscrollcommand =game_scroll.set)

my_game.pack()

game_scroll.config(command=my_game.yview)
game_scroll.config(command=my_game.xview)

my_game['columns'] = ('_id', '_name')

my_game.column("#0", width=0,  stretch=NO)
my_game.column("_id",anchor=CENTER, width=80)
my_game.column("_name",anchor=CENTER,width=80)

my_game.heading("#0",text="",anchor=CENTER)
my_game.heading("_id",text="Id",anchor=CENTER)
my_game.heading("_name",text="Step",anchor=CENTER)

#add data 
for i in range(40):
    my_game.insert(parent='',index='end',iid=i,text='',values=(str(i + 1),'Going to the step '+str(i + 1)))
my_game.pack()
ws.mainloop()

Pricipal problem is that horizontal scrollbar doesn´t work, so I am not able to see in what step is it going.

It only looks like this:

first image

I am just able to move down or up, but I am not able to move at left or right. So I hope someone can help me, thanks in advance.


Solution

  • You have vertical and horizontal scrollbars with the same name. Try changing code along the lines of the following snippet.

    game_scrollV = Scrollbar(game_frame, orient='vertical')
    game_scrollV.pack(side=RIGHT, fill=Y)
    
    game_scrollH = Scrollbar(game_frame,orient='horizontal')
    game_scrollH.pack(side= BOTTOM,fill=X)
    
    my_game = ttk.Treeview(
        game_frame,
        yscrollcommand = game_scrollV.set,
        xscrollcommand = game_scrollH.set)
    
    my_game.pack()
    
    game_scrollV.config(command=my_game.yview)
    game_scrollH.config(command=my_game.xview)
    
    my_game.column("_id",anchor=CENTER, minwidth = 100, width=80)
    my_game.column("_name",anchor=CENTER,minwidth = 100, width=80)