将Scrollbar添加到Tkinter中的Canvas时出现问题

时间:2016-07-18 19:11:58

标签: python canvas tkinter scrollbar tkinter-canvas

我试图在我的程序中向画布添加滚动条小部件。该程序基本上是一个表,通过向主服务器添加Canvas,然后使用.grid()将Frames添加到Canvas。我已经包含了代码的相关部分。我不确定我做错了什么。

from Tkinter import *    
master = Tk()
canvas = Canvas(master)
title_frame = Frame(canvas, bd=2, relief=RIDGE)
...
canvas.pack()
vbar = Scrollbar(master, orient=VERTICAL)
vbar.config(command=canvas.yview)
vbar.pack(side=RIGHT, fill=Y)
canvas.config(yscrollcommand=vbar.set)
master.mainloop()

This is what my program is producing.

1 个答案:

答案 0 :(得分:0)

当我打包画布时,我刚刚添加了side=LEFT,滚动条现在位于画布的右侧而不是下方。这是因为pack的默认方是TOP

from Tkinter import *    
master = Tk()
canvas = Canvas(master)
canvas.pack(side=LEFT)
vbar = Scrollbar(master, orient=VERTICAL)
vbar.config(command=canvas.yview)
vbar.pack(side=RIGHT, fill=Y)
canvas.config(yscrollcommand=vbar.set)
master.mainloop()

scrollbar

编辑:我想我现在理解你的问题:我试图显示水平和垂直滚动条,但我没有设法用包布局。我建议你改用网格:

from Tkinter import *    
master = Tk()
canvas = Canvas(master)
canvas.grid(row=0,column=0)
hbar = Scrollbar(master, orient=HORIZONTAL)
hbar.config(command=canvas.xview)
hbar.grid(row=1, column=0, sticky=E+W)
vbar = Scrollbar(master, orient=VERTICAL)
vbar.config(command=canvas.yview)
vbar.grid(row=0,column=1,sticky=N+S)

canvas.config(xscrollcommand=hbar.set, yscrollcommand=vbar.set)
master.mainloop()