0

I am trying to setup a scrollable canvas with tkinter:

from tkinter import *

class PopupTk(Frame):
    def __init__(self, master=None, title="Notification", msg="New information", duration=2):
        Frame.__init__(self, master)
        self.duration = duration

        # setup button
        close_button = Button(self, text="C", command=self.master.destroy)
        close_button.pack(side=LEFT)

        # set up scrollable canvas
        vscrollbar = Scrollbar(self, orient=VERTICAL)
        # vscrollbar.pack(fill=Y, side=RIGHT, expand=FALSE)
        vscrollbar.pack(side=LEFT)
        canvas = Canvas(self, bd=0, highlightthickness=0, yscrollcommand=vscrollbar.set)
        # canvas.pack(fill=BOTH, expand=TRUE)
        canvas.pack(side=LEFT)
        vscrollbar.config(command=canvas.yview)


        # setup text inside canvas
        title_label = Label(canvas, text=title, wraplength=250)
        title_label.config(justify=LEFT)
        title_label.pack(fill=X)
        msg_label = Label(canvas, text=msg, wraplength=250)
        msg_label.config(justify=LEFT)
        msg_label.pack(fill=X)

        self.pack(side=TOP, fill=BOTH, expand=YES, padx=10, pady=10)

        # get screen width and height
        ws = self.master.winfo_screenwidth()
        hs = self.master.winfo_screenheight()
        w = 300
        h = 100
        # calculate position x, y
        x = ws - w
        y = hs - h
        self.master.geometry('%dx%d+%d+%d' % (w, h, x, y))
        self.master.overrideredirect(True)
        self.master.lift()

    def auto_close(self):
        msec_until_close = self.duration * 1000
        self.master.after(msec_until_close, self.master.destroy)


if __name__ == '__main__':
    root = Tk()

    sp = PopupTk(root, msg="If you don’t specify a size, the label is made just large enough to hold its contents. If you don’t specify a size, the label is made just large enough to hold its contents. ", duration=33)
    sp.auto_close()
    root.call('wm', 'attributes', '.', '-topmost', True)
    root.mainloop()

But the scrollbar is not functional at all (not even quite visible):

enter image description here

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
qed
  • 22,298
  • 21
  • 125
  • 196

1 Answers1

0

The scrollbar won't work on widgets packed into a canvas. It will only scroll widgets added with create_window.

Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
  • Could you please give a small example? Thanks! – qed Nov 13 '14 at 12:59
  • These questions all have answers with examples: http://stackoverflow.com/q/3085696/7432, http://stackoverflow.com/q/16188420/7432, http://stackoverflow.com/q/8946814/7432 – Bryan Oakley Nov 13 '14 at 14:40