Python - Tkinter frame's Configure event constantly firing, callback constantly being called - python

I am fairly new to Tkinter and am trying to build an application that contains table data within a scrollable canvas. I have designed a Table class that contains a Canvas widget, which in turn contains a Frame that contains the table content.
However, when trying to make the Canvas scroll-able, I am running into an issue. I have bound the table content's Configure event to a callback that sets the scroll region. However, when I run my application this callback is called forever in an infinite loop, and I am not sure why. I cannot figure out why the Configure event would be constantly firing.
Moreover, when trying to switch between tabs, my app will freeze, presumably because of the app being stuck in a loop of callback calls.
Below is a simplified example of what I am trying to do in my app. Can anyone help me figure out how to fix this problem, while also allowing the table content to be scroll-able?
import tkinter as tk
from tkinter import ttk
class Table(ttk.Frame):
def __init__(self, parent):
ttk.Frame.__init__(self, parent)
self.table_canvas = tk.Canvas(self, bg='cyan')
table_scrollbar = ttk.Scrollbar(
self.table_canvas,
orient='vertical',
command=self.table_canvas.yview)
self.table_content = ttk.Frame(self.table_canvas)
self.table_content.bind('<Configure>', self.on_configure)
self.table_canvas.create_window(
(0, 0),
window=self.table_content,
anchor='nw')
self.table_canvas.configure(yscrollcommand=table_scrollbar.set)
for _ in range(10):
table_row = ttk.Frame(self.table_content)
ttk.Label(
table_row,
text='left text'
).grid(column=0, row=0, sticky='w')
ttk.Label(
table_row,
text='right text'
).grid(column=1, row=0, sticky='e')
table_row.columnconfigure(0, weight=1, uniform='same')
table_row.columnconfigure(1, weight=1, uniform='same')
table_row.pack(fill='x')
self.table_canvas.pack(side='left', fill='both', expand=True)
table_scrollbar.pack(side='right', fill='y')
def on_configure(self, event):
print('on configure triggered')
self.table_canvas.configure(scrollregion=self.table_canvas.bbox('all'))
app = tk.Tk()
app.geometry('600x600')
notebook = ttk.Notebook(app)
tab1 = ttk.Frame(notebook)
tab2 = ttk.Frame(notebook)
container1 = ttk.Frame(tab1)
label_container1 = ttk.Frame(container1)
ttk.Label(label_container1, text='Tab One').pack(side='left')
label_container1.pack(fill='x')
table1 = Table(container1)
table1.pack(fill='both', expand=True)
container1.pack(fill='both', expand=True)
container2 = ttk.Frame(tab2)
label_container2 = ttk.Frame(container2)
ttk.Label(label_container2, text='Tab Two').pack(side='left')
label_container2.pack(fill='x')
table2 = Table(container2)
table2.pack(fill='both', expand=True)
container2.pack(fill='both', expand=True)
notebook.add(tab1, text='One')
notebook.add(tab2, text='Two')
notebook.pack(fill='both', expand=True)
app.mainloop()

I'm not 100% sure of why this is happening, but the root cause seems to be that you're putting the scrollbar inside the canvas. As a rule of thumb, a scrollbar shouldn't be a child of the thing that it is scrolling.
If you move the scrollbar to be a child of the frame (self) instead of the canvas, the problem goes away.
table_scrollbar = ttk.Scrollbar(
self,
orient='vertical',
command=self.table_canvas.yview)

Related

Python tkinter window resizes, but widgets never change or move

Python beginner. I placed a scrollbar widget in window and that works, but no matter what I do I can't get the scrollbox widget to change size. Could go with a larger scrollbox or for it to resize when the window resizes, but can't figure out how to force either to happen. Tried lots of different solutions, but feels like the grid and canvas are defaulting to a size and can't figure out how to change that. Help would be appreciated. Code is below:
import tkinter as tk
from tkinter import ttk
import os
import subprocess
class Scrollable(tk.Frame):
"""
Make a frame scrollable with scrollbar on the right.
After adding or removing widgets to the scrollable frame,
call the update() method to refresh the scrollable area.
"""
def __init__(self, frame, width=16):
scrollbar = tk.Scrollbar(frame, width=width)
scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=True)
self.canvas = tk.Canvas(frame, yscrollcommand=scrollbar.set)
self.canvas.pack(side=tk.LEFT, fill=tk.BOTH, expand=True)
scrollbar.config(command=self.canvas.yview)
self.canvas.bind('<Configure>', self.__fill_canvas)
# base class initialization
tk.Frame.__init__(self, frame)
# assign this obj (the inner frame) to the windows item of the canvas
self.windows_item = self.canvas.create_window(0,0, window=self, anchor=tk.NW)
def __fill_canvas(self, event):
"Enlarge the windows item to the canvas width"
canvas_width = event.width
self.canvas.itemconfig(self.windows_item, width = canvas_width)
def update(self):
"Update the canvas and the scrollregion"
self.update_idletasks()
self.canvas.config(scrollregion=self.canvas.bbox(self.windows_item))
root = tk.Tk()
root.title("application")
root.geometry('750x800')
dbEnvs = ['a','b']
x = 1
header = ttk.Frame(root)
body = ttk.Frame(root)
footer = ttk.Frame(root)
header.pack(side = "top")
body.pack()
footer.pack(side = "top")
#setup Environment selection
envLabel = tk.Label(header, text="Environment:")
envLabel.grid(row=0,column=0,sticky='nw')
dbselection = tk.StringVar()
scrollable_body = Scrollable(body, width=20)
x = 1
for row in range(50):
checkboxVar = tk.IntVar()
checkbox = ttk.Checkbutton(scrollable_body, text=row, variable=checkboxVar)
checkbox.var = checkboxVar # SAVE VARIABLE
checkbox.grid(row=x, column=1, sticky='w')
x += 1
scrollable_body.update()
#setup buttons on the bottom
pullBtn = tk.Button(footer, text='Pull')
pullBtn.grid(row=x, column=2, sticky='ew')
buildBtn = tk.Button(footer, text='Build')
buildBtn.grid(row=x, column=3, sticky='ew')
compBtn = tk.Button(footer, text='Compare')
compBtn.grid(row=x, column=4, sticky='ew')
root.mainloop()
have tried anchoring, changing the window base size and multiple other things (8 or 19 different items, plus reading lots of posts), but they normally involve packing and since I used grids that normally and ends with more frustration and nothing changed.
If you want the whole scrollbox to expand to fill the body frame, you must instruct pack to do that using the expand and fill options:
body.pack(side="top", fill="both", expand=True)
Another problem is that you're setting expand to True for the scrollbar. That's probably not something you want to do since it means the skinny scrollbar will be allocated more space than is needed. So, remove that attribute or set it to False.
scrollbar.pack(side=tk.RIGHT, fill=tk.Y, expand=False)
tip: when debugging layout problems, the problems are easier to visualize when you temporarily give each widget a unique color. For example, set the canvas to one color, body to another, the instance of Scrollable to another, etc. This will let you see which parts are visible, which are growing or shrinking, which are inside the borders of others, etc.

How to shrink a frame in tkinter after removing contents?

Most of the topics I came across deals with how to not shrink the Frame with contents, but I'm interested in shrinking it back after the destruction of said contents. Here's an example:
import tkinter as tk
root = tk.Tk()
lbl1 = tk.Label(root, text='Hello!')
lbl1.pack()
frm = tk.Frame(root, bg='black')
frm.pack()
lbl3 = tk.Label(root, text='Bye!')
lbl3.pack()
lbl2 = tk.Label(frm, text='My name is Foo')
lbl2.pack()
So far I should see this in my window:
Hello!
My name is Foo
Bye!
That's great, but I want to keep the middle layer interchangeable and hidden based on needs. So if I destroy the lbl2 inside:
lbl2.destroy()
I want to see:
Hello!
Bye!
But what I see instead:
Hello!
███████
Bye!
I want to shrink frm back to basically non-existence because I want to keep the order of my main widgets intact. Ideally, I want to run frm.pack(fill=tk.BOTH, expand=True) so that my widgets inside can scale accordingly. However if this interferes with the shrinking, I can live without fill/expand.
I've tried the following:
pack_propagate(0): This actually doesn't expand the frame at all past pack().
Re-run frm.pack(): but this ruins the order of my main widgets.
.geometry(''): This only works on the root window - doesn't exist for Frames.
frm.config(height=0): Oddly, this doesn't seem to change anything at all.
frm.pack_forget(): From this answer, however it doesn't bring it back.
The only option it leaves me is using a grid manager, which works I suppose, but not exactly what I'm looking for... so I'm interested to know if there's another way to achieve this.
When you destroy the last widget within a frame, the frame size is no longer managed by pack or grid. Therefore, neither pack nor grid knows it is supposed to shrink the frame.
A simple workaround is to add a small 1 pixel by 1 pixel window in the frame so that pack still thinks it is responsible for the size of the frame.
Here's an example based off of the code in the question:
import tkinter as tk
root = tk.Tk()
lbl1 = tk.Label(root, text='Hello!')
lbl1.pack()
frm = tk.Frame(root, bg='black')
frm.pack()
lbl3 = tk.Label(root, text='Bye!')
lbl3.pack()
lbl2 = tk.Label(frm, text='My name is Foo')
lbl2.pack()
def delete_the_label():
lbl2.destroy()
if len(frm.winfo_children()) == 0:
tmp = tk.Frame(frm, width=1, height=1, borderwidth=0, highlightthickness=0)
tmp.pack()
root.update_idletasks()
tmp.destroy()
button = tk.Button(root, text="Delete the label", command=delete_the_label)
button.pack()
root.mainloop()
Question: Shrink a Frame after removing the last widget?
Bind to the <'Expose'> event and .configure(height=1) if no children.
Reference:
Expose
An Expose event is generated whenever all or part of a widget should be redrawn
import tkinter as tk
class App(tk.Tk):
def __init__(self):
super().__init__()
tk.Label(self, text='Hello!').pack()
self.frm = frm = tk.Frame(self, bg='black')
frm.pack()
tk.Label(self, text='Bye!').pack()
tk.Label(frm, text='My name is Foo').pack()
self.menubar = tk.Menu()
self.config(menu=self.menubar)
self.menubar.add_command(label='delete', command=self.do_destroy)
self.menubar.add_command(label='add', command=self.do_add)
frm.bind('<Expose>', self.on_expose)
def do_add(self):
tk.Label(self.frm, text='My name is Foo').pack()
def do_destroy(self):
w = self.frm
if w.children:
child = list(w.children).pop(0)
w.children[child].destroy()
def on_expose(self, event):
w = event.widget
if not w.children:
w.configure(height=1)
if __name__ == "__main__":
App().mainloop()
Question: Re-run frm.pack(): but this ruins the order of my main widgets.
frm.pack_forget(), however it doesn't bring it back.
Pack has the options before= and after. This allows to pack a widget relative to other widgets.
Reference:
-before
Use its master as the master for the slaves, and insert the slaves just before other in the packing order.
Example using before= and self.lbl3 as anchor. The Frame are removed using .pack_forget() if no children and get repacked at the same place in the packing order.
Note: I show only the relevant parts!
class App(tk.Tk):
def __init__(self):
...
self.frm = frm = tk.Frame(self, bg='black')
frm.pack()
self.lbl3 = tk.Label(self, text='Bye!')
self.lbl3.pack()
...
def on_add(self):
try:
self.frm.pack_info()
except:
self.frm.pack(before=self.lbl3, fill=tk.BOTH, expand=True)
tk.Label(self.frm, text='My name is Foo').pack()
def on_expose(self, event):
w = event.widget
if not w.children:
w.pack_forget()
Tested with Python: 3.5 - 'TclVersion': 8.6 'TkVersion': 8.6

tkinter Scrollbar collapses the Canvas it's in

I'm attempting to make a program with two sections. The left section will display a vertically scrollable list while the right section will display info based on the items selected in the list. Ignore the right section since I haven't gotten there yet.
Below is a general idea of what it'll look like except the left section will scroll vertically.
Unfortunately when I pack the scrollbar the left section completely disappears.
Below is the code.
import tkinter as tk
class Tasks(tk.Tk):
def __init__(self, builds=None):
super().__init__()
if builds is None:
self.builds = []
else:
self.builds = builds
self.title('Title')
self.geometry('1000x600')
self.configure(bg='red')
self.tasks_canvas = tk.Canvas(self, width=200, bg='green')
self.tasks_frame = tk.Frame(self.tasks_canvas)
self.scrollbar = tk.Scrollbar(self.tasks_canvas, orient='vertical',command=self.tasks_canvas.yview)
self.canvas_frame = self.tasks_canvas.create_window((0, 0), window=self.tasks_frame, anchor='n')
self.tasks_canvas.configure(yscrollcommand=self.scrollbar.set)
self.tasks_canvas.pack(side=tk.LEFT, fill=tk.Y)
self.scrollbar.pack(side=tk.LEFT, fill=tk.Y, expand=1)
if __name__ == '__main__':
root = Tasks()
root.mainloop()
I'm sure I'm missing a simply concept but I just can't figure it out.
The reason this is happening is because of the way pack geometry manager works. Have a look at this answer. Quoting it here:
By default pack will attempt to shrink (or grow) a container to exactly fit its children. Because the scrollbar is a children of the canvas in the, the canvas shrinks to fit.
To get around this, you can use an extra Frame to contain the Canvas and the Scrollbar and set the parent of the Scrollbar as this Frame.
import tkinter as tk
class Tasks(tk.Tk):
def __init__(self, builds=None):
super().__init__()
self.title('Title')
self.geometry('400x120')
self.configure(bg='red')
self.t_frame = tk.Frame(self)
self.t_frame.pack(side=tk.LEFT)
self.tasks_canvas = tk.Canvas(self.t_frame, width=100, bg='green')
self.scrollbar = tk.Scrollbar(self.t_frame, orient='vertical',command=self.tasks_canvas.yview)
self.tasks_canvas.configure(yscrollcommand=self.scrollbar.set)
self.tasks_canvas.pack(side=tk.LEFT, fill=tk.Y)
self.scrollbar.pack(side=tk.LEFT, fill=tk.Y)
if __name__ == '__main__':
root = Tasks()
root.mainloop()

Tkinter treeview widgets not properly aligned/added space between widgets

I am working on this table in tkinter made from a bunch of treeveiw widgets. The idea is to get a table where I can add lines, select lines and edit them. In the code below you can add lines to the table by pushing the button. I now want to control the height of each row by configuring the style. However, when I use style the alignment of the treeview widgets is messed up, see attached picture. Any suggestions how to fix this?
EDIT: The problem is the added space between the widgets.
The code for the table is:
from tkinter import *
from tkinter import ttk
class MyApp(Tk):
def __init__(self):
super(MyApp, self).__init__()
self.geometry('950x500+100+100')
self.NewTree = []
label = Label(self,text='Table with some data', font=("Arial Bold", 25))
label.pack()
self.addLine()
master_frame = Frame(self, bd=3, relief=RIDGE)
master_frame.pack(side=BOTTOM)
# Create a frame for the canvas and scrollbar(s).
frame2 = Frame(master_frame)
frame2.pack(side = BOTTOM)
# Add a canvas in that frame.
self.canvas = Canvas(frame2)
self.canvas.grid(row=0, column=0)
# Create a vertical scrollbar linked to the canvas.
vsbar = Scrollbar(frame2, orient=VERTICAL, command=self.canvas.yview)
vsbar.grid(row=0, column=1, sticky=NS)
self.canvas.configure(yscrollcommand=vsbar.set)
# Create a frame on the canvas to contain the buttons.
self.table_frame = Frame(self.canvas)
# Create canvas window to hold the buttons_frame.
self.canvas.create_window((0,0), window=self.table_frame, anchor=NW)
def addLine(self):
#Make button for adding step
bt = Button(self,text='Add Line',command=lambda: self.addLineMethod())
bt.config(width=9, height=1)
bt.pack()
def addLineMethod(self):
lineNumber = int(len(self.NewTree)/5)
for index in range(5):
s = ttk.Style()
s.configure('MyStyle.Treeview', rowheight=25)
self.NewTree.append(ttk.Treeview(self.table_frame, height=1,show="tree",columns=("0"),style='MyStyle.Treeview'))
#Works fine when using this line instead of those above
#self.NewTree.append(ttk.Treeview(self.table_frame, height=1,show="tree",columns=("0")))
self.NewTree[index+5*lineNumber].grid(row=lineNumber, column=index+1)
self.NewTree[index+5*lineNumber]['show'] = ''
item = str(index+5*lineNumber)
self.NewTree[index+5*lineNumber].column("0", width=180, anchor="w")
self.NewTree[index+5*lineNumber].insert("", "end",item,text=item,values=('"Text text text"'))
self.table_frame.update_idletasks() # Needed to make bbox info available.
bbox = self.canvas.bbox(ALL) # Get bounding box of canvas with Buttons.
# Define the scrollable region as entire canvas with only the desired
# number of rows and columns displayed.
self.canvas.configure(scrollregion=bbox, width=925, height=200)
app = MyApp()
app.mainloop()
Her is a picture of the table with some lines.
Put the style configuration in the __init__() function and the effect will go away. I'm not clear as to why this works.
def __init__(self):
...
s = ttk.Style()
s.configure('MyStyle.Treeview', rowheight=20)

How to get an all sticky grid of Treeview and Scrollbar in Python Tkinter?

What I want in Tkinter in Python 2.7 is the following grid layout:
However once, I start using the grid() functions instead of pack() functions, nothing is showing on running the script. The following is what I am stuck with:
import Tkinter, ttk
class App(Tkinter.Frame):
def __init__(self,parent):
Tkinter.Frame.__init__(self, parent, relief=Tkinter.SUNKEN, bd=2)
self.parent = parent
self.grid(row=0, column=0, sticky="nsew")
self.menubar = Tkinter.Menu(self)
try:
self.parent.config(menu=self.menubar)
except AttributeError:
self.tk.call(self.parent, "config", "-menu", self.menubar)
self.tree = ttk.Treeview(self.parent)
self.tree.grid(row=0, column=0, sticky="nsew")
self.yscrollbar = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
self.yscrollbar.grid(row=0, column=1, sticky='nse')
self.tree.configure(yscrollcommand=self.yscrollbar.set)
self.yscrollbar.configure(command=self.tree.yview)
if __name__ == "__main__":
root = Tkinter.Tk()
root.title("MyApp")
app = App(root)
app.pack()
app.mainloop()
Any help will be highly appreciated.
You have several problems that are affecting your layout.
First, some of the widgets inside App use self as the parent, some use self.parent. They should all use self in this particular case. So, the first thing to do is change the parent option of the Treeview to self.
self.tree = ttk.Treeview(self)
Second, since your main code is calling app.pack(), you shouldn't be calling self.grid. Remove the line `self.grid(row=0, column=0, sticky="nsew"). It's redundant.
Third, you are using very unusual code to add the menubar. You need to configure the menu of the root window. There's no need to put this in a try/except block, and there's no reason to use self.tk.call. Simply do this:
self.parent.configure(menu=self.menubar)
This assumes that self.parent is indeed the root window. If you don't want to force that assumption you can use winfo_toplevel() which will always return the top-most window:
self.parent.winfo_toplevel().configure(menu=self.menubar)
Finally, since you are using grid, you need to give at least one row and one column a "weight" so tkinter knows how to allocate extra space (such as when the user resizes a window).
In your case you want to give all of the weight to row and column 0 (zero), since that's where you've placed the widget which needs the most space:
def __init__(self, parent):
...
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
Note: you'll also want to make sure when you call app.pack() that you give it parameters that makes it fill any extra space, too. Otherwise the tree will fill "app", but "app" would not fill the window.
app.pack(fill="both", expand=True)
Here is a fully working example with all of those changes. I grouped the main layout code together since that makes the code easier to visualize and easier to maintain:
import Tkinter, ttk
class App(Tkinter.Frame):
def __init__(self,parent):
Tkinter.Frame.__init__(self, parent, relief=Tkinter.SUNKEN, bd=2)
self.parent = parent
self.menubar = Tkinter.Menu(self)
self.parent.winfo_toplevel().configure(menu=self.menubar)
self.tree = ttk.Treeview(self)
self.yscrollbar = ttk.Scrollbar(self, orient='vertical', command=self.tree.yview)
self.tree.configure(yscrollcommand=self.yscrollbar.set)
self.tree.grid(row=0, column=0, sticky="nsew")
self.yscrollbar.grid(row=0, column=1, sticky='nse')
self.yscrollbar.configure(command=self.tree.yview)
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
if __name__ == "__main__":
root = Tkinter.Tk()
root.title("MyApp")
app = App(root)
app.pack(fill="both", expand=True)
app.mainloop()
Your question mentioned grid, but in this case you could save a few lines of code by using pack. pack excels in layouts like this, where your gui is aligned top-to-bottom and/or left-to-right. All you need to do is replace the last five lines (the calls to grid, grid_rowconfigureandgrid_columnconfigure`) with these two lines:
self.yscrollbar.pack(side="right", fill="y")
self.tree.pack(side="left", fill="both", expand=True)

Categories

Resources