Reputation: 23
I started today with Tkinter and I run into some problems. My goal is to open a window which loads a csv-file and outputs the column names as buttons and the rows as labels for each value. Now I want to be able to scroll down the labels.
I looked into this subject and I understand now that I need to frame my Button and Labels before I am able to add a scrollbar. With the code below, I am not able to scroll and the scrollbar goes down to the end of the list(instead of the end of the window).
For now my output looks like this:Output of window
with following error message:
TclError: unknown option "-yscrollcomand"
My code:
def open():
# add new Window
root1 = Toplevel()
root1.title("this_is_a_first_try.csv")
root1.iconbitmap("logo.ico")
root1.configure(bg= "white")
root1.geometry("1000x500")
# add Data Frame
data = pd.read_csv("this_is_a_first_try.csv")
df = pd.DataFrame(data)
#create canvas
canvas_main = Canvas(root1, bg= "gray")
canvas_main.grid(row= 2, column= 1, sticky= "news", columnspan= len(df.columns))
for index,title in enumerate(df.columns): # generating Buttons for headers of column
if index == 0:
Button(canvas_main, text= "Index").grid(row= 2, column= 1, sticky= W+E) # generating header for index-column
else:
Button(canvas_main, text= title).grid(row= 2, column= index + 1, sticky= W+E)
for index1, row in enumerate(df.values): # generating Label-Widgets for values
for index2, value in enumerate(row):
Label(canvas_main, text= value, anchor= W).grid(row= index1 + 3, column= index2 + 1, sticky= W+E)
scrollbar = Scrollbar(root1, orient= "vertical", command= canvas_main.yview)
scrollbar.grid(row= 2, column= len(df.columns) + 1, sticky= "ns")
canvas_main.configure(yscrollcomand= scrollbar.set)
I can't find any solution in the internet, If anyone could give me a hint what to look for, I'd be grateful.
Thanks in advance.
Upvotes: 0
Views: 1226
Reputation: 2270
You can use a listbox. Below is a simple example with a listbox:
from tkinter import *
window = Tk()
window.geometry("500x500")
s = Scrollbar(window)
s.pack(side = RIGHT, fill = Y)
l = Listbox(window, height = 500, width = 500)
l.pack()
for i in range(1000):
l.insert(END, i)
l.config(yscrollcommand = s.set)
s.config(command = l.yview)
window.mainloop()
You can just get all the elements in .csv file, and then put those in the list. Then, just use a for loop to insert all of the elements into the listbox, and done!!
Hope this helps!
Upvotes: 1