Reputation: 23
I am trying to take a frame and insert it into a canvas that has a scrollbar because the data does not fit entirely on it's own window. This is my code that writes the frame, but don't know how to put it into a canvas and correctly write a scrollbar to sit on the right side of the window:
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
b = tk.Button(self, text="Done!", command=self.upload_cor)
b.grid()
table = tk.Frame(self, width=300, height=300)
table.grid(row=0,column=0)
canvas=Canvas(table,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
vbar=Scrollbar(table,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)
data = (word_freq)
self.widgets = {}
row = 0
for word, freq in (data):
row += 1
self.widgets[word] = {
"Word": tk.Label(table, text=word),
"Freq": tk.Label(table, text=freq),
"checkbox_var": tk.IntVar(),
"checkbox": tk.Checkbutton(table)
}
self.widgets[word]["Word"].grid(row=row, column=1, sticky="nsew")
self.widgets[word]["Freq"].grid(row=row, column=2, sticky="nsew")
self.widgets[word]["checkbox"].config(variable=(self.widgets[word]["checkbox_var"]))
self.widgets[word]["checkbox"].grid(row=row, column=7, sticky="nsew")
table.grid_columnconfigure(1, weight=1)
table.grid_columnconfigure(2, weight=1)
# invisible row after last row gets all extra space
table.grid_rowconfigure(row+1, weight=1)
def upload_cor(self):
for word in sorted(self.widgets.keys()):
entry_widget = self.widgets[word]["num_seconds_correction"]
new_value = entry_widget.get()
print("%s: %s" % (word, new_value))
for word in sorted(self.widgets.keys()):
check_var = self.widgets[word]["checkbox_var"]
print(check_var.get())
if check_var.get():
print('Check button checked')
if __name__ == "__main__":
root = tk.Tk()
Example(root).grid(fill="both", expand=True)
root.mainloop()
Upvotes: 2
Views: 2347
Reputation: 1
from tkinter import *
pj = Tk()
pj.geometry("500x600")
fin = Frame(pj, width=200, height=200 )
fin.pack()
fin.place(x=90, y = 50)`
canvas=Canvas(fin,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
vbar=Scrollbar(fin,orient=VERTICAL)
vbar.pack(side=RIGHT,fill=Y)
vbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(yscrollcommand=vbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)
Upvotes: 0
Reputation: 2690
The following demonstrates the gridding of a canvas and scrollbar within a frame. The canvas contains the table frame that in turn contains the other gridded widgets.
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent)
b = tk.Button(self, text="Done!", command=self.upload_cor)
b.grid()
canvas=Canvas(self,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
vbar=Scrollbar(self,orient=VERTICAL)
vbar.grid(row=1,column=1,sticky='nsw')
vbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(yscrollcommand=vbar.set)
canvas.grid(row=1,column=0,sticky='news')
table = tk.Frame(canvas, width=300, height=300)
canvas.create_window(0,0,anchor='nw',height=300,width=300,window=table)
data = (word_freq)
self.widgets = {}
row = 0
for word, freq in (data):
row += 1
self.widgets[word] = {
"Word": tk.Label(table, text=word),
"Freq": tk.Label(table, text=freq),
"checkbox_var": tk.IntVar(),
"checkbox": tk.Checkbutton(table)
}
self.widgets[word]["Word"].grid(row=row, column=1, sticky="nsew")
self.widgets[word]["Freq"].grid(row=row, column=2, sticky="nsew")
self.widgets[word]["checkbox"].config(variable=(self.widgets[word]["checkbox_var"]))
self.widgets[word]["checkbox"].grid(row=row, column=7, sticky="nsew")
table.grid_columnconfigure(1, weight=1)
table.grid_columnconfigure(2, weight=1)
# invisible row after last row gets all extra space
table.grid_rowconfigure(row+1, weight=1)
def upload_cor(self):
for word in sorted(self.widgets.keys()):
entry_widget = self.widgets[word]["num_seconds_correction"]
new_value = entry_widget.get()
print("%s: %s" % (word, new_value))
for word in sorted(self.widgets.keys()):
check_var = self.widgets[word]["checkbox_var"]
print(check_var.get())
if check_var.get():
print('Check button checked')
if __name__ == "__main__":
root = tk.Tk()
Example(root).grid(sticky='news')
root.mainloop()
Upvotes: 3