Reputation: 11
I am new in Python and stackoverFlow. I am trying to create Checkbox widget automatically from input first column CSV file. How can i get scroll bar in right position. The image below before input the CSV file, scroll bar in the right place
The image below after input the CSV file, checkbox widgets can't be scroll
CODE using python 3.5
import tkinter as tk
from tkinter import ttk
import start
from tkinter.filedialog import askopenfilename
import numpy as np
import pandas as pd
LARGE_FONT=("Verdana", 12)
class MainPage(tk.Frame):
"""docstring for MainPage"""
def __init__(self, parent,controller):
tk.Frame.__init__(self,parent)
self.controller = controller
self.getColumn = []
self.createWidgets()
def createWidgets(self):
self.createPanel()
def createPanel(self):
nb = ttk.Notebook(self)
nb.grid(row=1,column=0,columnspan=50,rowspan=49,sticky="NEWS")
self.createMainPageTab(nb)
self.createClassifyTab(nb)
def createMainPageTab(self,nb):
self.featureSelection = tk.IntVar()
page1 = ttk.Frame(nb)
nb.add(page1,text="Preprocessing")
selectFileFrame = tk.LabelFrame(page1,text = "Select File")
selectFileFrame.grid(row=0,column=0,sticky="W",padx=5,pady=5,ipady=5,columnspan=10)
selectRoleFrame = tk.LabelFrame(page1,text = "select Role")
selectRoleFrame.grid(row=1,column=0,sticky="W",padx=5,pady=5,ipady=5,columnspan=1,rowspan=1)
text = tk.Label(selectFileFrame,text="Select The File")
text.grid(row=0,column=0, sticky="E")
textSelectFile = tk.Text(selectFileFrame,width = 20, height = 1)
textSelectFile.grid(row=0,column=1,sticky="WE",pady=3,padx=5)
buttonSelectFile = tk.Button(selectFileFrame,text="browse...",command=lambda : self.load_file(textSelectFile,frameCheckBox))
buttonSelectFile.grid(row=0,column=2,sticky="w")
checkButtonFeatureSelection = tk.Checkbutton(selectFileFrame, text= "Feature Selection",variable = self.featureSelection)
checkButtonFeatureSelection.grid(row=1,column=0,sticky="w")
buttonCek = tk.Button(selectFileFrame, text='Show', command= lambda : self.var_states())
buttonCek.grid(row=2,column=0,sticky="W")
#create canvas for scroll bar
canvasScroll = tk.Canvas(selectRoleFrame,bg="Yellow")
canvasScroll.grid(row=0,column=0)
vBar = tk.Scrollbar(selectRoleFrame,orient="vertical",command=canvasScroll.yview)
vBar.grid(row=0,column=1, sticky="ns")
canvasScroll.configure(yscrollcommand=vBar.set)
frameCheckBox = tk.Frame(canvasScroll,bg="blue",bd=2,relief=tk.GROOVE)
canvasScroll.create_window((0,0), window=frameCheckBox,anchor="nw")
frameCheckBox.bind("<Configure>",self.resize(canvasScroll))
def resize(self,canvasScroll):
canvasScroll.configure(scrollregion=canvasScroll.bbox("all"),width=235,height=90)
def readingCsv(self,fname):
readCsv = pd.read_csv(fname)
return readCsv
#event input csv file
def load_file(self,textSelectFile,frameCheckBox):
fname = askopenfilename(filetypes=(("*.csv","Template files"),
("HTML files", "*.html;*.htm"),
("All files", "*.*") ))
print(fname)
mlabel = tk.Label(self,text="%s" % fname)
mlabel.grid(row=0,column=3)
# getText=self.textSelectFile.get()
textSelectFile.configure(state=tk.NORMAL)
textSelectFile.delete(1.0,tk.END)
textSelectFile.insert(tk.END,fname)
textSelectFile.configure(state=tk.DISABLED)
read = self.readingCsv(fname)
self.getColumn = self.getColumnFromCsv(read)
#create checkbox
if self.getColumn:
for Column in self.getColumn:
v = tk.StringVar()
l = tk.Checkbutton(frameCheckBox,text=Column,variable=v)
l.grid(sticky="w")
def getColumnFromCsv(self,readCsv):
numeric_variables = list(readCsv.dtypes[readCsv.dtypes != "object"].index)
return numeric_variables
def var_states(self):
alabel = tk.Label(self,text="%s" % self.featureSelection.get())
alabel.grid(row=3,column=0)
print("tes : %d" %(self.featureSelection.get()))
def feature_selection(self,chcekFeatureSelection):
if self.featureSelection.get() == 1:
pass
def createClassifyTab(self,nb):
page2 = ttk.Frame(nb)
nb.add(page2,text="Classify")
Upvotes: 0
Views: 895
Reputation: 142879
You have
frameCheckBox.bind("<Configure>",self.resize(canvasScroll))
which means
result = self.resize(canvasScroll)
frameCheckBox.bind("<Configure>", result)
so you didn't assign function but you executed resize
and assigned its result.
But resize
returns None
so finally you got
frameCheckBox.bind("<Configure>", None)
bind()
(similar to command=
) expects callback - it means function name without ()
and arguments - like this
frameCheckBox.bind("<Configure>", self.resize)
Because you want to send extra argument (and skip event
send by bind
as default) you have to use lambda
frameCheckBox.bind("<Configure>", lambda event: self.resize(canvasScroll))
Upvotes: 1