Roger Almengor
Roger Almengor

Reputation: 464

Cannot add a scrollbar to main window in Tkinter

I am creating a Graphic User Interface with the library tkinter (Python 3).

The following Code is what I have written so far, so the GUI is called and there are also some Buttons created but the functionallity attached to Events have not been implemented except the Help button which is the object of this post.

import tkinter
from tkinter import *
import tkinter.filedialog
from tkinter.filedialog import askopenfilename
from tkinter.filedialog import askdirectory
import PIL
from PIL import Image, ImageTk


class Window(Frame):


    def __init__(self, master = None):

        self.master = master 
        self.f18 = Frame(master, width=300, height =20)
        self.f17 = Frame(master, width=300, height =20)
        self.ftf18 = Frame(master, width=300, height =20)
        self.ftf17 = Frame(master, width = 300, height = 20)
        self.sf = Frame(master, width=300, height=30, relief=GROOVE, borderwidth = 4, bg = "SlateGray1")
        self.sf_17 = Frame(master, width=300, height=30, relief=GROOVE, borderwidth = 4, bg = "SlateGray1")
        self.tf_18 = Frame(master, width=300, height=30, relief=GROOVE, borderwidth = 4, bg = "SlateGray2")
        self.tf_17 = Frame(master, width=300, height=30, relief=GROOVE, borderwidth = 4, bg = "SlateGray2")
        self.output_folder = Frame(master, width = 300, height = 30, relief = GROOVE, borderwidth = 4)
        self.blank_frame_before_last = Frame(master, width = 300, height = 30)
        self.run_help_quit_frame = Frame(master, width = 300, height = 30, relief = GROOVE, borderwidth = 4)


        self.tcm_name_18 = ''
        self.tcm_name_17 = ''
        self.tf_name_18 = ''
        self.tf_name_17 = ''
        self.out_folder = ''

        self.var = IntVar()

        def browse_button_tcm18():
            filename = askopenfilename(filetypes = (("GEOTIFF Files", "*.tif"),))
            self.file_name = filename 
            self.display_tcm_18.config(text = filename)
            print(self.file_name)

        def browse_button_tcm17():
            filename = askopenfilename(filetypes = (("GEOTIFF Files", "*.tif"),))
            self.file_name = filename 
            self.display_tcm_17.config(text = filename)
            print(self.file_name)

        def browse_button_tf18():
            filename = askopenfilename(filetypes = (("GEOTIFF Files", "*.tif"),))
            self.file_name = filename 
            self.display_tf_18.config(text = filename)
            print(self.file_name)

        def browse_button_tf17():
            filename = askopenfilename(filetypes = (("GEOTIFF Files", "*.tif"),))
            self.file_name = filename 
            self.display_tf_17.config(text = filename)
            print(self.file_name)

        def browse_folder():
            folder_name = askdirectory()
            self.output_folder = folder_name
            self.display_output_folder.config(text = folder_name)
            print(self.folder_name)

        def run():
            pass

        def quit():
            pass 

        def help():
            help_window = Toplevel(master)
            help_window.title("Help")
            help_window.geometry("500x800")
            app_h = Help_Window(help_window)
            help_window.mainloop()

        self.blank_label_0 = Label(self.master, text = "").pack(side=TOP, padx=5)
        self.blank_label = Label(self.sf, text = "", bg="SlateGray1").pack(side=TOP, padx=5)
        self.open_tcm_button = Button(self.sf, text = "Open..", command = browse_button_tcm18).pack(side=LEFT, padx = 5, pady = 10)
        self.display_tcm_18 = Label(self.sf, width = 80, bg = "white", textvariable = self.tcm_name_18, relief = SUNKEN, anchor = W)
        self.display_tcm_18.pack(side=LEFT)
        self.tcm18_label = Label(self.sf, text = "Input1", bg="SlateGray1", relief = GROOVE).place(relx = 0.70, rely = 0.20,anchor=W)#.place(relx=0.75, rely=0.04,anchor=W)
        self.sf.pack(side=TOP)
        self.f18.pack(side=TOP)

        self.blank_label_2 = Label(self.sf_17, text = "",  bg="SlateGray1").pack(side=TOP, padx=5)
        self.open_tcm_17_button = Button(self.sf_17, text = "Open..", command = browse_button_tcm17).pack(side=LEFT, padx = 5, pady = 10)
        self.display_tcm_17 = Label(self.sf_17, width = 80, bg = "white", textvariable = self.tcm_name_17, relief = SUNKEN, anchor = W)
        self.display_tcm_17.pack(side=LEFT)
        self.tcm_17_label = Label(self.sf_17, text = "Input2", bg="SlateGray1", relief = GROOVE).place(relx = 0.70, rely = 0.20,anchor=W)
        self.sf_17.pack(side=TOP)
        self.f17.pack(side=TOP)

        self.blank_label_3 = Label(self.tf_18, text = "", bg = "SlateGray2").pack(side=TOP, padx=5)
        self.open_tf_18_button = Button(self.tf_18, text = "Open..", command = browse_button_tf18).pack(side=LEFT, padx = 5, pady = 10)
        self.display_tf_18 = Label(self.tf_18, width = 80, bg = "white", textvariable = self.tf_name_18, relief = SUNKEN, anchor = W)
        self.display_tf_18.pack(side=LEFT)
        self.tf_18_label = Label(self.tf_18, text = "Input3", bg="SlateGray2", relief = GROOVE).place(relx = 0.70, rely = 0.20,anchor=W)
        self.tf_18.pack(side=TOP)
        self.ftf18.pack(side=TOP)

        self.blank_label_4 = Label(self.tf_17, text = "", bg = "SlateGray2").pack(side=TOP, padx=5)
        self.open_tf_17_button = Button(self.tf_17, text = "Open..", command = browse_button_tf17).pack(side=LEFT, padx = 5, pady = 10)
        self.display_tf_17 = Label(self.tf_17, width = 80, bg = "white", textvariable = self.tf_name_17, relief = SUNKEN, anchor = W)
        self.display_tf_17.pack(side=LEFT)
        self.tf_17_label = Label(self.tf_17, text = "Input4", bg="SlateGray2", relief = GROOVE).place(relx = 0.70, rely = 0.20,anchor=W)
        self.tf_17.pack(side=TOP)
        self.ftf17.pack(side = TOP)

        self.blank_label_5 = Label(self.output_folder, text = "").pack(side=TOP, padx = 5)
        self.open_output_folder_button = Button(self.output_folder, text = "Open..", command = browse_folder).pack(side = LEFT, padx = 5, pady = 10)
        self.display_output_folder = Label(self.output_folder, width = 80, bg = "white", textvariable = self.out_folder, relief = SUNKEN, anchor = W) 
        self.display_output_folder.pack(side = LEFT) 
        self.output_folder_label = Label(self.output_folder, text = "Output Folder", relief = GROOVE).place(relx = 0.70, rely = 0.20, anchor = W)
        self.output_folder.pack(side = TOP)

        self.blank_frame_before_last.pack(side = TOP)
        self.run_help_quit_frame.pack(side = TOP)
        self.quit_button = Button(self.run_help_quit_frame, text = "Run!", command = run).pack(side=LEFT, padx = 5, pady = 10)
        self.blank_label_6 = Label(self.run_help_quit_frame, text = " "*15).pack(side=LEFT, padx=5)
        self.run_button = Button(self.run_help_quit_frame, text = "Quit!", command = quit).pack(side = LEFT, padx = 5, pady = 10)
        self.blank_label_7 = Label(self.run_help_quit_frame, text = " "*15).pack(side=LEFT, padx=5)
        self.help_button = Button(self.run_help_quit_frame, text = "Help?", command = help).pack(side = LEFT, padx = 5, pady = 10)


class Help_Window(Window):


    def __init__(self, master = None):
        self.master = master

        #Adding scrollbar
        self.scrollbar = Scrollbar(self.master, orient = 'vertical')
        self.scrollbar.pack(side = RIGHT, fill = 'y')

        # Creating Title
        self.description_label = Label(master, text = "\nHelp for Tool",
                                font = "Verdana 14 bold").pack(side = TOP)

        # Creating introduction
        introduction = """\n
        This tool provides a basic workflow.\n
        """
        self.introduction_text = Label(master, width= 70, height = 5, justify = CENTER, padx = 5, 
                                        text = introduction, font = "Verdana 10").pack(side=TOP)

        # Adding TCM 2018
        self.tcm_2018_label = Label(master, text = "\nInput1\n", font = "Verdana 12 bold").pack(
                                    side = TOP)
        self.tcm_2018_img_canvas = Canvas(master, bg = "black", height = 300, width = 300)
        self.tcm_2018_img_canvas.pack(side = TOP)

        # Adding TCM 2017
        self.tcm_2017_label = Label(master, text = "\nInput 2\n", font = "Verdana 12 bold").pack(
                                    side = TOP)
        self.tcm_2017_img_canvas = Canvas(master, bg = "black", height = 300, width = 300)
        self.tcm_2017_img_canvas.pack(side = TOP)
        self.scrollbar.config(command = self.tcm_2017_img_canvas.yview)
        self.tcm_2017_img_canvas.config(yscrollcommand = self.scrollbar.set)
        #self.tcm_2017_img_canvas.pack()



root = Tk() 
root.title("Tool Title")
root.geometry("900x800")
app = Window(root) 
root.mainloop()

When Pressing the Help button, you see that a Kind of Help window is called. I tried creating a Scrollbar attached to this new window. The scrollbar is displayed but it does not work. I've been searching in some documents and also some stack Overflow Posts, and I found the following Information which I Need to confirm with you.

Adding a scrollbar to a group of widgets in Tkinter

http://effbot.org/zone/tkinter-scrollbar-patterns.htm

Is it the case I am doing the Things wrong since I am creating a Help_Window class and adding the a scrollbar to the master window, and not doing it the other way around which is create first a canvas and then adding a scrollbar and other Frames where I will write my Content? I Need this clarification, before undertaking the Task of modiying the Help_Window class.

Upvotes: 0

Views: 83

Answers (1)

Robot Mind
Robot Mind

Reputation: 321

You already solved your problem. window object cant be master of scrollbar. Possible masters of scrollbar can be canvas, text, listbox or even entry in some cases. I think got all of them. So if you want use scroll bar with practically whole window just make one "window" canvas that will contain scrollbar and all other elements of that window

Upvotes: 3

Related Questions