1

This is a code to choose columns I need and create file by that. But the canvas is too small to get all columns if there are more than 20 columns. and getting out of the canvas.I tried to add the scroll bar but its not working at all.I am a newbie into python.Thanks in advance.

import tkinter as tk
from tkinter import filedialog
import pandas as pd    

tried for scrolling

def on_configure(event):
    canvas1.configure(scrollregion=canvas1.bbox('all'))    

def browse():#to browse the file path
    global f
    global export_path
    global export_path_ing

file opening

    import_file_path = filedialog.askopenfilename() 
    f = pd.read_csv (import_file_path, index_col = 0 , low_memory=False ,encoding="unicode_escape" ,delimiter=',')
    export_path=import_file_path[0:-4]+"_OUTPUT.csv"  #to create the output path
    export_path_ing=import_file_path[0:-4]+"_INGEST_OUTPUT.txt"        

    dirs = list(f.columns.values)

    # remove previous IntVars
    intvar_dict.clear()

    # remove previous Checkboxes
    for cb in checkbutton_list:
        cb.destroy()
    checkbutton_list.clear() 

    for filename in dirs:
        # create IntVar for filename and keep in dictionary
        intvar_dict[filename] = tk.IntVar()

        # create Checkbutton for filename and keep on list
        c = tk.Checkbutton(canvas1, text=filename, variable=intvar_dict[filename])
        c.pack()
        checkbutton_list.append(c)

def test():
    li=[]
    for key, value in intvar_dict.items():
        if value.get() > 0:
            li.append(key)
            print(li)

    f2=f[li]
# --- main -
    f2.to_csv ( export_path,index = None, header=True ,sep=',')
    f2.to_csv (export_path_ing,index = None, header=True ,sep='\t')

intvar_dict = {}
 # to keep all Checkbuttons for all filenames
checkbutton_list = []

window = tk.Tk()
window.title("Column Choices          ~~~~~sarsuman")
window.geometry("500x500")
window.option_add("*Button.Background", "black")
window.option_add("*Button.Foreground", "white")
#frame = tk.Frame(window)
canvas1 = tk.Canvas(window, width = 1000, height = 1000, bg = 'orange',highlightthickness=1)

scrollbar = tk.Scrollbar(window,command=canvas1.yview)
scrollbar.pack(side = 'right', fill ='y')
canvas1.configure(yscrollcommand = scrollbar.set)
canvas1.pack(side='left',expand=True,fill='both')
canvas1.bind('<Configure>', on_configure)
frame = tk.Frame(canvas1)
canvas1.create_window((0,0), window=frame, anchor='nw')

#canvas1.config(xscrollcommand=tk.hbar.set, yscrollcommand=tk.vbar.set)    

#back = tk.Frame(master=window,bg='black')
#back.pack_propagate(0) #Don't allow the widgets inside to determine the frame's width / height
#back.pack(fill=tk.BOTH, expand=1) #Expand the frame to fill the root window

lbl = tk.Label(canvas1, text="Path")
lbl.pack()

ent1 = tk.Entry(canvas1)
ent1.pack()

btn1 = tk.Button(canvas1, text="Select Path", command=browse)
btn1.pack()

btn1 = tk.Button(canvas1, text="Test Checkboxes", command=test)
btn1.pack()
window.mainloop()  
window.quit
Paul Rooney
  • 20,879
  • 9
  • 40
  • 61

1 Answers1

0

This post should answer most of your questions in making your scrollbar work, I used it in the past to get mine working

Tkinter scrollbar for frame

It looks to me like you are trying to add the scrollbar to the parent tk instance, I think you have to add it to a frame rather than that 'window'.

this is another similar question

tkinter: using scrollbars on a canvas

John T
  • 234
  • 1
  • 6