我在按下下面代码中的按钮后添加了小部件。现在我想添加滚动条来滚动添加的小部件,但滚动条不起作用。有人可以发布相同的工作代码,因为我无法找到解决方案。
from Tkinter import *
def create_line ():
""" """
global N
Label (canvas, text= "Color ").grid(row=N, column=0, padx=3)
OptionMenu (canvas, v, *optionList).grid(row=N, column=1, padx=3)
Button (canvas, text="+", command=lambda:add_line()).grid(row=N, column=2, padx=3)
def add_line ():
global N
N = N +1
Label (canvas, text= "Color ").grid(row=N, column=0, padx=3)
OptionMenu (canvas, v, *optionList).grid(row=N, column=1, padx=3)
canvas.config()
root = Tk()
frame=Frame(root,width=300,height=300)
frame.grid(row=0,column=0)
canvas=Canvas(frame,bg='#FFFFFF',width=300,height=300,scrollregion=(0,0,500,500))
scrollbar = Scrollbar(frame,orient=VERTICAL)
scrollbar.pack(side=RIGHT,fill=Y)
scrollbar.config(command=canvas.yview)
canvas.config(width=300,height=300)
canvas.config(yscrollcommand=scrollbar.set)
canvas.pack(side=LEFT,expand=True,fill=BOTH)
optionList = ("red", "green", "blue")
N = 0
v = StringVar()
v.set(optionList[0])
create_line()
mainloop()