Python Tkinter滚动条画布不起作用

时间:2016-12-20 09:10:33

标签: tkinter scrollbar python-3.5 tkinter-canvas

我无法让滚动条适用于tkinter框架。我希望能够滚动的框架是由导入的SQL内容组成的网格。我已将此框架包含在画布中(如其他帖子中所示)。

当我加载特定屏幕时,画布会在非常小的空间中加载,其中包含几个SQL内容标签和后面的滚动条。使用滚动条时,画布会展开以填充适当的大小,并且它可以正常工作。

以下是使用滚动条之前和之后的两个屏幕截图:

在使用滚动之前

before scroll used

滚动后使用

after scroll used

以下是此特定框架的代码:

def drawBoardMain(): #BOARDS
global displayName
global body
global root
global mainFont

body=Frame(root)

headFrame=Frame(body) #heading

logoSource=PhotoImage(file='holcombe.gif')
logo=Label(headFrame,image=logoSource)
logo.logoSource=logoSource
logo.grid(row=0,rowspan=2,column=0)

heading=Label(headFrame,text='Display Management System - Board Management',font=(mainFont,24)).grid(row=0,rowspan=2,column=1)

loginLabel=Label(headFrame,text='Logged in as:',font=(mainFont,12)).grid(row=0,column=2,sticky=S)

userLabel=Label(headFrame,text=displayName,font=(mainFont,12)).grid(row=1,column=2,sticky=N)

headFrame.pack() #heading

mainFrame=Frame(body) #main body

introLabel=Label(mainFrame,text='Select a board to view more details or make changes, or click the button below to add a new one',font=(mainFont,14)).grid(column=0,row=0)

containingCanvas=Canvas(mainFrame,height=500,width=900) #canvasOPEN

myscrollbar=Scrollbar(containingCanvas,orient="vertical",command=containingCanvas.yview) #scrollbarOPEN

containingCanvas.configure(yscrollcommand=myscrollbar.set)
containingCanvas.configure(scrollregion = containingCanvas.bbox("all"))
myscrollbar.pack(fill='y',side=RIGHT) #scrollbarCLOSE

containingCanvas.grid(column=0,row=1,pady=15) #canvasCLOSE

users=Frame(containingCanvas) #board browserOPEN

con=sql.connect('datastore.db')
cur=con.cursor()
cur.execute('SELECT boardID FROM tblBoards')
boards=cur.fetchall()
cur.execute('SELECT locationDescription FROM tblBoards')
locationDescriptions=cur.fetchall()
cur.execute('SELECT department FROM tblBoards')
departments=cur.fetchall()
cur.execute('SELECT comments FROM tblBoards')
comments=cur.fetchall()
con.close()

for i in range(len(boards)):
    a=Button(users,text=boards[i],font=(mainFont,14),width=20,padx=3,command=lambda x=boards[i][0]:handleBoardEdit(x))
    a.grid(column=0,row=i+1,sticky=NSEW,padx=1)
for j in range(len(locationDescriptions)):
    b=Label(users,text=locationDescriptions[j],font=(mainFont,14),padx=3,relief=RAISED)
    b.grid(row=j+1,column=1,sticky=NSEW,padx=1)
for k in range(len(departments)):
    c=Label(users,text=departments[k],font=(mainFont,14),padx=3,relief=RAISED)
    c.grid(row=k+1,column=2,sticky=NSEW,padx=1)
for l in range(len(comments)):
    d=Label(users,text=comments[l],font=(mainFont,14),padx=3,relief=RAISED)
    d.grid(row=l+1,column=3,sticky=NSEW,padx=1)

uLabel=Label(users,text="Board ID",font=(mainFont,16),padx=3,relief=RAISED).grid(column=0,row=0,sticky=NSEW)
fLabel=Label(users,text="Location",font=(mainFont,16),padx=3,relief=RAISED).grid(column=1,row=0,sticky=NSEW)
sLabel=Label(users,text="Department",font=(mainFont,16),padx=3,relief=RAISED).grid(column=2,row=0,sticky=NSEW)
rLabel=Label(users,text="Comments",font=(mainFont,16),padx=3,relief=RAISED).grid(column=3,row=0,sticky=NSEW)

users.bind("<Configure>", lambda event, containingCanvas=containingCanvas: containingCanvas.configure(scrollregion=containingCanvas.bbox("all")))
users.pack(fill='both',side=LEFT) #board browserCLOSE    
containingCanvas.create_window((4,4),window=users, anchor="nw")


addButton=Button(mainFrame,text='Add New',font=(mainFont,14),command=handleAddBoard).grid(column=0,row=2)

mainFrame.pack() #main body

footFrame=Frame(body) #footer

logoutButton=Button(footFrame,text='Back to Menu',width='7',height='2',wraplength='80',font=(mainFont,14),command=returnMainHandle).grid(column=0,row=0,pady=15)

footFrame.pack(side=BOTTOM) #footer

body.pack(fill=BOTH,expand=1)

我估计我可能只是错误的订单。任何帮助表示赞赏。

我知道这段代码非常低效,我可能会更好地创建一个使用对象的解决方案,但是这在目前是不可行的,所以程序性的答案只能谢谢:)

0 个答案:

没有答案