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):