滚动条不起作用,如何获取画布区域。画布大小随其中按钮的数量而变化
我已经在tk(root)中创建了一个框架,一个框架中的一个小部件创建了顶层窗口,该窗口在一个框架中有多个Button.Button的数量随列表而变化。那个框架(里面有按钮)存在于画布中。我的问题是在放置滚动小部件画布后不要移动
from tkinter import *
root = Tk()
root.geometry("500x200")
my_app= Frame(root)
my_app.pack()
my_window = Toplevel(my_app, bg='brown')
my_window.geometry("500x200+300+500")
top_window = Frame(my_window, bd=2, relief=SUNKEN)
top_window.grid_columnconfigure(0, weight=1)
yscrollbar = Scrollbar(top_window)
yscrollbar.grid(row=0, column=1, sticky=N+S)
canvas = Canvas(top_window, bd=0, yscrollcommand=yscrollbar.set)
canvas.config(scrollregion=(0, 0, 500, 1000))
canvas.grid(row=0, column=0, sticky=N+S+E+W)
yscrollbar.config( command = canvas.yview)
top_window.pack()
my_f = Frame(canvas)
def ins_ind(m):
print(m)
results =
["one","two","three","four","five","six","seven","eight","nine","ten"]
ins_list=[]
for ind, result in enumerate(results):
ins=str(result)
ins_list.append(ind)
ins_list[ind] = Button(my_f, text = ins, font='Times 12 bold',
bg='sandy brown',anchor=E, fg="black", command = lambda m=ins:
ins_ind(m) )
ins_list[ind].pack()
my_f.pack()
root.mainloop()
滚动条不动
答案 0 :(得分:0)
要在滚动画布时移动按钮,必须将按钮放在画布上。
您的代码将按钮放在框架my_f
上。要将按钮放在画布上,应使用:canvas.create_window(x, y, window=button)
。