我想创建一个带有按钮的框架,它也应该是可滚动的。我能够创建一个可滚动的框架(第一个),但是当我尝试制作一个按钮并创建一个命令(依次创建一个框架)(第二个)时,却无法正常工作。
请有人帮我
第一个
from tkinter import *
root=Tk()
frame=Frame(root,width=1000,height=800)
frame.grid(row=0,column=0)
frame.grid_propagate(0)
canvas=Canvas(frame,bg='red',width=900,height=500)
hbar=Scrollbar(frame,orient=HORIZONTAL)
hbar.grid(row=1,column=0,rowspan=1,columnspan=1,sticky=W+E)
hbar.config(command=canvas.xview)
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.grid(row=0,column=1,rowspan=1,columnspan=1,sticky=NE+SE)
vbar.config(command=canvas.yview)
#def click():
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.grid(row=0,column=0)
canvas.grid_propagate(0)
global frame_1
frame_1=Frame(canvas)
canvas.create_window((0,0),window=frame_1,anchor=NW)
canvas.bind('<Configure>',lambda e:canvas.configure(scrollregion=canvas.bbox('all')))
for j in range(100):
Label(frame_1,text="Label{}".format(j)).grid(row=j+5,column=j)
#def exit():
#frame_1.destroy()
Button(frame,text="Append",command=click).grid(row=2,column=0,sticky=NW)
Button(frame,text="Exit",command=exit).grid(row=3,column=0,sticky=NW)
root.mainloop()
第二个:
from tkinter import *
root=Tk()
frame=Frame(root,width=1000,height=800)
frame.grid(row=0,column=0)
frame.grid_propagate(0)
canvas=Canvas(frame,bg='red',width=900,height=500)
hbar=Scrollbar(frame,orient=HORIZONTAL)
hbar.grid(row=1,column=0,rowspan=1,columnspan=1,sticky=W+E)
hbar.config(command=canvas.xview)
vbar=Scrollbar(frame,orient=VERTICAL)
vbar.grid(row=0,column=1,rowspan=1,columnspan=1,sticky=NE+SE)
vbar.config(command=canvas.yview)
def click():
canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
canvas.grid(row=0,column=0)
canvas.grid_propagate(0)
global frame_1
frame_1=Frame(canvas)
canvas.create_window((0,0),window=frame_1,anchor=NW)
canvas.bind('<Configure>',lambda e:canvas.configure(scrollregion=canvas.bbox('all')))
for j in range(100):
Label(frame_1,text="Label{}".format(j)).grid(row=j+5,column=j)
#def exit():
#frame_1.destroy()
Button(frame,text="Append",command=click).grid(row=2,column=0,sticky=NW)
Button(frame,text="Exit",command=exit).grid(row=3,column=0,sticky=NW)
root.mainloop()