[英]What is wrong with my setup of scrollable canvas in tkinter?
I am trying to setup a scrollable canvas with tkinter: 我正在尝试使用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): 但是滚动条根本不起作用(甚至看不到):
The scrollbar won't work on widgets packed into a canvas. 滚动条不适用于包装在画布中的小部件。 It will only scroll widgets added with
create_window
. 它只会滚动用
create_window
添加的小部件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.