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()
Related
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.
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)
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
I'd like to create an easy way to add scrollbars to any frame I like. So far, only one works. What's wrong with this script? What's a proper way to do this? I still only have a faint grasp of all the concepts hidden in this, sorry.
import Tkinter as tk
def data(parent):
for i in range(50):
tk.Label(parent,text=i).grid(row=i,column=0)
tk.Label(parent,text="my text"+str(i)).grid(row=i,column=1)
tk.Label(parent,text="..........").grid(row=i,column=2)
class ScrollBar():
#def __init__(self, tk):
# self.canvas = tk.Canvas()
def myfunction(self, event):
self.canvas.configure(scrollregion=self.canvas.bbox("all"), width=200, height=200)
print("orig: ",self.canvas)
def makeScrollBar(self, tk, parent):
self.outerframe = tk.Frame(parent, relief="groove", width=50, height=100, bd=1)
self.outerframe.pack()
self.canvas = tk.Canvas(self.outerframe)
self.innerframe = tk.Frame(self.canvas)
self.myscrollbar = tk.Scrollbar(self.outerframe, orient="vertical", command=self.canvas.yview)
self.canvas.configure(yscrollcommand=self.myscrollbar.set)
self.myscrollbar.pack(side="right",fill="y")
self.canvas.pack(side="left")
self.canvas.create_window((0,0), window=self.innerframe, anchor="nw")
self.innerframe.bind("<Configure>", self.myfunction)
print("orig: ",self.canvas)
return self.innerframe
root = tk.Tk()
root.wm_geometry("800x600+100+100")
scrollbargenerator = ScrollBar()
b = scrollbargenerator.makeScrollBar(tk, root)
c = scrollbargenerator.makeScrollBar(tk, root)
data(b)
data(c)
root.mainloop()
First, you manage to create your scrollbars. If you scroll your mouse over the first scrollbar it will actually scroll the canvas. The thumb does not appear (or do not move) because the scrollregion is never set for this canvas.
In fact, your use of classes and objects is broken. Your Scrollbar class is instantiated once and this only instance update its fields each time makeScrollBar is called. Thus in myfunction callback, self.canvas always refer to the lastly created canvas.
You can easily fix your code my using different scrollbar generators
scrollbargenerator = ScrollBar()
b = scrollbargenerator.makeScrollBar(tk, root)
scrollbargenerator = ScrollBar()
c = scrollbargenerator.makeScrollBar(tk, root)
or capturing the canvas in a closure
def myfunction(self, canvas):
canvas.configure(scrollregion=canvas.bbox("all"), width=200, height=200)
def makeScrollBar(self, tk, parent):
#(...)
self.innerframe.bind("<Configure>", (lambda canvas: (lambda event: self.myfunction(canvas)))(self.canvas))
or relying on the information already present in event
#staticmethod
def myfunction(event):
event.widget.master.configure(scrollregion=event.widget.master.bbox("all"), width=200, height=200)
I want to get a table, but when I realized, that there's no table in python, I decided to fix it with two listboxes.
The point is, that I don't want a border between them.
So my Question is: How to remove the border from the Tk Listbox in Python?
Even if it'll become white I had a solution...
You want to set the borderwidth to zero, but you also want to set highlightthickness to zero. Highlightthickness represents a rectangle that surrounds the whole widget when it has keyboard focus. Finally, when you use pack or grid, make sure you don't add any padding between them.
If you want to complete the illusion that the two widgets are one, put them in a frame and give the frame a borderwidth and relief.
import Tkinter as tk
class Example(tk.Frame):
def __init__(self, parent):
tk.Frame.__init__(self, parent, borderwidth=1, relief="sunken")
lb1 = tk.Listbox(self, borderwidth=0, highlightthickness=0)
lb2 = tk.Listbox(self, borderwidth=0, highlightthickness=0)
lb1.pack(side="left", fill="both", expand=True)
lb2.pack(side="left", fill="both", expand=True)
lb1.insert(0, "left")
lb2.insert(0, "right")
if __name__ == "__main__":
root = tk.Tk()
Example(root).pack(fill="both", expand=True, padx=8, pady=8)
root.mainloop()
I think the best you can achieve would be this:
import tkinter as tk
root = tk.Tk()
wrapper = tk.Frame(root, bd=2, relief="sunken")
L1 = tk.Listbox(wrapper, bd=0)
L2 = tk.Listbox(wrapper, bd=0)
L1.grid(column = 1, row = 1)
L2.grid(column = 2, row = 1)
wrapper.pack()
root.mainloop()
note setting the border of each listbox to 0, (bd=0) and to give the overall widget a similar look to the original listbox i've wrapped it in a frame and given that the same border style as the default listbox.
also worth nothing that you need to get the bindings right to make them scroll as expected, just binding to the scroll wheel and scroll bar is insufficient as the lists can be moved with the arrow keys when an item is highlighted, like in the second example on this page:
scrolling multiple listboxes together
by Santiclause
Speicfy borderwidth as 0 when you create a listbox.
For example:
from Tkinter import * # from tkinter import * (Python 3.x)
root = Tk()
lb = Listbox(borderwidth=0) # <---
lb.pack()
root.mainloop()