tkinter - how to add scrollbar to all tabs (canvas) - python

How can i add scrollbar to my Frame in tkinter? I would like to add scrollbar in Comments_tab.
I was trying to play with Canvas, however, i am doing something wrong. Could i ask for your advice please?
class Main(tk.Tk):
def __init__(self):
#
tk.Tk.__init__(self)
#make notebook fill display
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
#Notebook
nb = ttk.Notebook(self)
nb.grid(row=0, column=0, sticky='nswe')
#keep a reference to the pages
self.p0 = Config_tab(self)
self.p1 = Comments_tab(self)
#Canvas
canvas_comments=tk.Canvas(self.p1,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
vbar=tk.Scrollbar(self.p1,orient=tk.VERTICAL)
vbar.grid(row=0, column=0, sticky="NSE")
vbar.config(command=canvas_comments.yview)
tk.Label(self.p1, text="asdddddd").grid(row=0,column=0)
canvas_comments.config(width=300,height=300)
canvas_comments.config(yscrollcommand=vbar.set)
canvas_comments.grid(row=0, column=0, sticky="NSEW")
#tabs
nb.add(self.p0, text="Config")
nb.add(self.p1, text="Comments")
class Comments_tab(tk.Frame):
def __init__(self, master, **kwargs):
tk.Frame.__init__(self, master, **kwargs)
tk.Label(self, text="HU3: ").grid(row=3, column=0)
if __name__ == "__main__":
root = Main()
root.geometry('800x600')
root.title("HAT")
root.mainloop()

Related

_tkinter.TclError: bad window path name "GreenFrame"

class View(tkinter.Tk):
def __init__(self):
tkinter.Tk.__init__(self)
self.geometry("500x500")
self.title("Switch Frame Example")
container_frame = tkinter.Frame(self)
container_frame.pack(side="top", fill="both", expand=True)
container_frame.grid_rowconfigure(0, weight=1)
container_frame.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (RedFrame, GreenFrame):
frame_name = F.__name__
frame = F(container_frame, self)#container_frame is the parent, self (the window) is the controller
self.frames[frame_name] = frame
frame.grid(row=0, column=0, sticky="nsew")
self.show_frame("RedFrame")
def show_frame(self, frame_name):
frame = self.frames[frame_name]
frame.tkraise()
class RedFrame(tkinter.Frame):
def __init__(self, parent, window):
tkinter.Frame.__init__(self, parent, bg="red", width="500", height="500")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_propagate(0)
goto_green_button = tkinter.Button(self, text='green frame', fg="green", width=25, command=lambda: window.tkraise("GreenFrame"))
goto_green_button.grid(row=0, column=0)
class GreenFrame(tkinter.Frame):
def __init__(self, parent, window):
tkinter.Frame.__init__(self, parent, bg="green", width="500", height="500")
self.grid_rowconfigure(0, weight=1)
self.grid_columnconfigure(0, weight=1)
self.grid_propagate(0)
goto_red_button = tkinter.Button(self, text='red frame', fg="red", width=25, command=lambda: window.tkraise("RedFrame"))
goto_red_button.grid(row=0, column=0)
The title of this post is the error I get when trying I click the goto_green_button. I used Bryan Oakley's code taken from this post as the basis for mine, but I'm not sure what I've done wrong, since his code actually works as it should, while mine does not. Why is "GreenFrame" a bad window path? It has the exact same name as the "GreenFrame" class, so shouldn't it work? Or does it have to do with the order of the frames and which is above the other?

How to prevent multiple windows from popping up in tkinter?

import tkinter as tk
from tkinter import *
from tkinter import ttk
LARGE_FONT = ("Verdana", 12)
class pages(tk.Tk):
#starts us off in the login page
def __init__(self, *args, **kwargs):
tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.wm_title(self, "ScanNET")
tk.Tk.wm_minsize(self, 800, 800)
container = tk.Frame(self)
container.pack(side=TOP, fill=BOTH, expand=True)
container.grid_rowconfigure(0, weight=1)
container.grid_columnconfigure(0, weight=1)
self.frames = {}
for F in (loginpage, GUI):
frame = F(container, self)
self.frames[F] = frame
frame.grid(row=0, column=0, sticky=N+E+S+W)
self.show_frame(loginpage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class loginpage(tk.Frame):
#login page content
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
loginlabel = tk.Label(self, text="login page", font=LARGE_FONT)
loginlabel.pack(padx=10, pady=10)
#button moves you to gui
loginbutton1 = tk.Button(self, text= "Go to GUI", command=lambda: controller.show_frame(GUI))
loginbutton1.pack()
class GUI(tk.Frame):
def __init__(self, parent, controller):
#all widths and heights aren't official, most likely change
tk.Frame.__init__(self, parent)
self.root = tk.Tk()
#the tabs
my_notebook = ttk.Notebook(self.root)
my_notebook.pack()
devicestab = Frame(my_notebook, width=800, height=600)
reportstab = Frame(my_notebook, width=800, height=600)
devicestab.pack(fill=BOTH, expand=1)
reportstab.pack(fill=BOTH, expand=1)
my_notebook.add(devicestab, text="Devices")
my_notebook.add(reportstab, text="Reports")
#contents for devices tab
devicesleft = LabelFrame(devicestab, text="Devices found: ", padx=5, pady=5, width=500, height=600)
devicesleft.grid(row=0, column=0)
devicesright = LabelFrame(devicestab, text="Activity Feed: ", padx=5, pady=5, width=300 , height=600)
devicesright.grid(row=0, column=1)
#contents for reports tab
reportsleft = LabelFrame(reportstab, text="Report Summaries: ", padx=5, pady=5, width=400 , height=600)
reportsleft.grid(row=0, column=0)
reportsright= LabelFrame(reportstab, text="Charts and Diagrams: ", padx=5, pady=5, width=400 , height=600)
reportsright.grid(row=0, column=1)
app = pages()
app.mainloop()
When I run this, both the loginpage and GUI windows open. Correct me if I'm wrong, but I think the problem is probably around the
tk.Frame.__init__(self, parent)
self.root = tk.Tk()
my_notebook = ttk.Notebook(self.root)
part in the GUI class. I've searched everywhere and I can't seem to find a way to have a first page as a login page which will move to a second page that has tabs using notebook. I feel as if something else has to be in the ttk.Notebook() part, and perhaps remove self.root = tk.Tk() after. I'd love to hear what y'all think.
I am assuming you want the notebook in the same widget of the rest, so you should not use tk.Tk() and then you place the notebook in the parent which is already your root. Check the code in the end of my answer. Also, since there was a lot of problems with your code I made some changes and comments that will help you to write better codes in tkinter. Please read it carefully. You may also want to study the effbot web page.
import tkinter as tk
# from tkinter import * # just don't do this
from tkinter import ttk
LARGE_FONT = ("Verdana", 12)
# class pages(tk.Tk):
class Pages(tk.Tk): # class names should start with upper case
#starts us off in the login page
# def __init__(self, *args, **kwargs):
def __init__(self):
# tk.Tk.__init__(self, *args, **kwargs)
tk.Tk.__init__(self)
# tk.Tk.wm_title(self, "ScanNET")
self.winfo_toplevel().title("ScanNET")
# tk.Tk.wm_minsize(self, 800, 800)
self.wm_minsize(800, 800) # since you defined tk.Tk as pages parent you can call Tk methods directly
container = tk.Frame(self)
# container.pack(side=TOP, fill=BOTH, expand=True)
# container.grid_rowconfigure(0, weight=1)
# container.grid_columnconfigure(0, weight=1)
container.grid(row=0, column = 0) # don't use pack if you want to use grid
self.frames = {}
for F in (loginpage, GUI):
frame = F(container, self)
self.frames[F] = frame
# frame.grid(row=0, column=0, sticky=N+E+S+W)
frame.grid(row=0, column=0, sticky='NESW') #since we are not importing all we are not importing tk.W but you can use string instead
self.show_frame(loginpage)
def show_frame(self, cont):
frame = self.frames[cont]
frame.tkraise()
class loginpage(tk.Frame):
#login page content
def __init__(self, parent, controller):
tk.Frame.__init__(self,parent)
loginlabel = tk.Label(self, text="login page", font=LARGE_FONT)
loginlabel.pack(padx=10, pady=10)
#button moves you to gui
loginbutton1 = tk.Button(self, text= "Go to GUI", command=lambda: controller.show_frame(GUI))
loginbutton1.pack()
class GUI(tk.Frame):
def __init__(self, parent, controller):
#all widths and heights aren't official, most likely change
tk.Frame.__init__(self, parent)
# self.root = tk.Tk() # don't create new Tk objects, you just need one. The others should be Toplevel objects
### self.root = tk.Toplevel() ### this would be the correct way of creating a new window but you don't want to do that here your root is your parent
#the tabs
# my_notebook = ttk.Notebook(self.root)
my_notebook = ttk.Notebook(self) # this is how you place the notebook in the Frame widget and not in a new one
# my_notebook.pack()
my_notebook.grid() # we are now using grid so it will not accept pack anymore
# devicestab = Frame(my_notebook, width=800, height=600)
devicestab = tk.Frame(my_notebook, width=800, height=600) # again, since we are not importing al we have to use tk. before tkinter methods
# reportstab = Frame(my_notebook, width=800, height=600)
reportstab = tk.Frame(my_notebook, width=800, height=600)
# devicestab.pack(fill=BOTH, expand=1)
devicestab.pack(fill="both", expand=1) # instead of tk.BOTH we can use "both"
reportstab.pack(fill="both", expand=1)
my_notebook.add(devicestab, text="Devices")
my_notebook.add(reportstab, text="Reports")
#contents for devices tab
devicesleft = tk.LabelFrame(devicestab, text="Devices found: ", padx=5, pady=5, width=500, height=600)
devicesleft.grid(row=0, column=0)
devicesright = tk.LabelFrame(devicestab, text="Activity Feed: ", padx=5, pady=5, width=300 , height=600)
devicesright.grid(row=0, column=1)
#contents for reports tab
reportsleft = tk.LabelFrame(reportstab, text="Report Summaries: ", padx=5, pady=5, width=400 , height=600)
reportsleft.grid(row=0, column=0)
reportsright= tk.LabelFrame(reportstab, text="Charts and Diagrams: ", padx=5, pady=5, width=400 , height=600)
reportsright.grid(row=0, column=1)
app = Pages()
app.mainloop()

How to add a scrollbar that orientes its position on the top most widget

I have a tkinter app in which I have a main canvas with multiple pages (all of which are frames). I pull up the different pages by rasing them with the frame.tkraise() command. I now want to add a scrollbar to the whole thing. The scrollbar appears but without a slider and I am not sure if it cna recognize the change of page.
import tkinter as tk
from tkinter import ttk
class Economics(tk.Tk):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
tk.Tk.columnconfigure(self, 0, weight=1)
tk.Tk.rowconfigure(self, 0, weight=1)
self.container = tk.Canvas()
self.container.grid(row=0, column=0, sticky="nsew")
self.container.columnconfigure("all", weight=1)
self.container.rowconfigure("all", weight=1)
self.vscrollbar = tk.Scrollbar(orient="vertical", command=self.container.yview)
self.container.configure(yscrollcomman=self.vscrollbar.set)
self.vscrollbar.grid(row=0, column=1, sticky="ns")
self.frames = {}
for F in (StartPage, ExamplePage1, ExamplePage2): # TUPLE OF PAGES
frame = F(self.container, self)
self.frames[F] = frame
self.show_frame(StartPage)
def show_frame(self, cont):
self.container.delete("all")
frame = self.frames[cont]
self.container.create_window(0, 0, anchor="nw", window=frame)
class StartPage(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
button_1 = ttk.Button(self, text="Example Page 1",
command=lambda: controller.show_frame(ExamplePage1))
button_1.grid(row=0, column=0)
button_2 = ttk.Button(self, text="Example Page 2",
command=lambda: controller.show_frame(ExamplePage2))
button_2.grid(row=1, column=0)
class ExamplePage1(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
for i in range(50):
label = tk.Label(self, text="Button {} of 50".format(i+1))
label.grid(row=i, column=0)
button_back = ttk.Button(self, text="Back",
command=lambda: controller.show_frame(StartPage))
button_back.grid(row=0, column=1)
class ExamplePage2(tk.Frame):
def __init__(self, parent, controller):
super().__init__(parent)
for i in range(35):
label = tk.Label(self, text="Button {} of 35".format(i+1))
label.grid(row=i, column=0)
button_back = ttk.Button(self, text="Back",
command=lambda: controller.show_frame(StartPage))
button_back.grid(row=0, column=1)
app = Economics()
app.geometry("800x600")
app.resizable(True, True)
app.mainloop()
In this example file you can see the basic structure of my app with some example widgets and buttons. The scrollbar shows but without the slider. What do I have to change to get a working scrollbar for all pages.
Later on I'm planning to get a horizontal scrollbar as well.
You can't scroll items added to a canvas with pack, place, or grid. A canvas can only scroll items added via the canvas create_* functions, such as create_window.

Why Tkinter application does not close after call to exit

I am developping an application with tkinter. It is made of several frame (windows) put one onto the other. I can pass from one window to the other with a push button. I built it upon an example I saw here on Stack overflow, but I cannot recall the link to it.
I suspect that due to this choice I made, the tkinter does not close properly after I call the method self.quit().
The example below is a simplified version of my app, although it has the same structure than the full app.
With the code below, you can see that the application won't close before the end of the full program, i.e. after the 10 sec delay I put.
import tkinter as tk
import tkinter.ttk as ttk
import time
class App:
"""
Inherited from the Frame class
"""
def __init__(self):
self.root = tk.Tk()
self.root.title('Test')
# This frame will contain all windows
container = ttk.Frame(self.root)
container.pack(side="top", fill="both", expand=True)
# Create different pages/windows of the application
self.frames = {}
self.frames["Window1"] = Window1(parent=container, controller=self)
self.frames["Window2"] = Window2(parent=container, controller=self)
self.frames["Window3"] = Window3(parent=container, controller=self)
self.frames["Window1"].grid(row=0, column=0, sticky="nsew")
self.frames["Window2"].grid(row=0, column=0, sticky="nsew")
self.frames["Window3"].grid(row=0, column=0, sticky="nsew")
self.show_frame("Window1")
def run(self):
self.root.deiconify
self.root.mainloop()
def show_frame(self, page_name):
'''
Show a frame for the given page name
'''
frame = self.frames[page_name]
frame.tkraise()
class Window1(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.controller = controller
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
# Go to window 2
self.buttonW2 = ttk.Button(self, text='Go to 2', command=lambda: controller.show_frame("Window2"))
self.buttonW2.grid(row=0, column=0, sticky='NSE')
# Go to window 3
self.buttonW3 = ttk.Button(self, text='Go to 3', command=lambda: controller.show_frame("Window3"))
self.buttonW3.grid(row=0, column=1, sticky='NSE')
# EXIT button
self.buttonExit = ttk.Button(self, text='Exit', command=self.quit)
self.buttonExit.grid(row=0, column=2, sticky='NSE')
class Window2(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.controller = controller
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
# Go back button
buttonBack = ttk.Button(self, text="Back to 1", command=lambda: controller.show_frame("Window1"))
buttonBack.grid(row=0, column=3, sticky="NSE")
class Window3(ttk.Frame):
def __init__(self, parent, controller):
ttk.Frame.__init__(self, parent)
self.controller = controller
self.columnconfigure(0, weight=1)
self.rowconfigure(0, weight=1)
# Go back button
buttonBack = ttk.Button(self, text="Back to 1", command=lambda: controller.show_frame("Window1"))
buttonBack.grid(row=1, column=1, sticky="E")
if __name__ == '__main__':
app = App()
app.run()
print('The App should be closed')
time.sleep(10)
print('The App is now closed')
Does anyone can help me for this?
Thanks
Tkinters quit command just stops the mainloop but doesn't destroy the window. If you want the window to be removed use destroy.
So create the exitbutton like:
self.buttonExit = ttk.Button(self, text='Exit', command=self.controller.root.destroy)
self.buttonExit.grid(row=0, column=2, sticky='NSE')

How to make listbox fill whole frame using grid manager?

Consider the following tk interface;
There are two frames, each in its own column managed by grid geometry manager. I tried several sticky options, but I couldn't make the listbox look longer. I want it to span acroos the whole row. How can I achieve it? Here are my codes;
import tkinter as tk
class MinimalTestCase(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master)
self.f1 = tk.Frame(self)
self.f2 = tk.Frame(self)
self.f1.grid(row=0, column=0)
self.f2.grid(row=0, column=1, sticky=(tk.N, tk.S, tk.E, tk.W))
### Fill left frame with dummy elements to demonstrate the problem
for i in range(15):
tk.Label(self.f1, text="Label{}".format(i)).grid(row=i)
### Put listbox on right frame
self.lbox = tk.Listbox(self.f2)
self.lbox.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.E, tk.W))
self.grid()
if __name__ == "__main__":
root=tk.Tk()
MinimalTestCase(root)
root.mainloop()
You have to use rowconfigure(0, weight=1) - not with Listbox but with its parent which manage this geometry (Frame).
import Tkinter as tk
class MinimalTestCase(tk.Frame):
def __init__(self, master, *args, **kwargs):
tk.Frame.__init__(self, master)
self.f1 = tk.Frame(self)
self.f2 = tk.Frame(self)
self.f2.rowconfigure(0, weight=1) # <-- row 0 will be resized
self.f1.grid(row=0, column=0)
self.f2.grid(row=0, column=1, sticky=(tk.N, tk.S, tk.E, tk.W))
### Fill left frame with dummy elements to demonstrate the problem
for i in range(15):
tk.Label(self.f1, text="Label{}".format(i)).grid(row=i)
### Put listbox on right frame
self.lbox = tk.Listbox(self.f2)
self.lbox.grid(row=0, column=0, sticky=(tk.N, tk.S, tk.E, tk.W))
self.grid()
if __name__ == "__main__":
root=tk.Tk()
MinimalTestCase(root)
root.mainloop()
Tkinterbook: The Tkinter Grid Geometry Manager

Categories

Resources