Tkinter中画布上的滚动条不起作用

时间:2019-04-02 13:05:02

标签: python canvas tkinter scroll

是的,对此有很多stackoverflow问题,我看到了所有内容,但无法正常工作,所以我在用我的代码提出一个新问题:

基本上我想在tkinter中创建一个具有4列的表。但我希望能够滚动此行,因为稍后将从mysql数据库中获取行。但我无法使滚动条正常工作。

这是我的代码:

from tkinter import *

root = Tk()
root.geometry("700x600")

frame_canvas = Frame(root)
frame_canvas.rowconfigure(0, weight=1)
frame_canvas.columnconfigure(0, weight=1)
frame_canvas.grid(row=0, column=0, sticky="news")

canvas = Canvas(frame_canvas)
canvas.grid(row=0, column=0, sticky="news")

vsb = Scrollbar(frame_canvas, orient="vertical", command=canvas.yview)
vsb.grid(row=0, column=1, sticky='ns')
canvas.configure(yscrollcommand=vsb.set)

for i in range(0, 50):
    for j in range(0, 4):
        canvas.columnconfigure(j, minsize=150)
        Label(canvas, text="hello"+str(i)+str(j)).grid(row=i, column=j)

canvas.config(scrollregion=canvas.bbox("all"))

app = root
root.mainloop()

我如何使画布滚动?还有另一种创建表的方法吗?另外,如果以后我想在表格底部添加一行,怎么可能?还是我必须清除整个画布并重新渲染?

编辑 在画布上添加了一个框架窗口。仍然无法滚动。那可能是现在的问题吗?

from tkinter import *

root = Tk()
root.geometry("700x600")

frame_canvas = Frame(root)
frame_canvas.rowconfigure(0, weight=1)
frame_canvas.columnconfigure(0, weight=1)
frame_canvas.grid(row=0, column=0, sticky="news")

canvas = Canvas(frame_canvas, height=600, width=500)
canvas.grid(row=0, column=0, sticky="news")

vsb = Scrollbar(frame_canvas, command=canvas.yview, orient="vertical")
vsb.grid(row=0, column=1, sticky='ns')
canvas.configure(yscrollcommand=vsb.set)

frame2 = Frame(canvas, bg="powder blue", height=600, width=700)
canvas.create_window((0, 0), anchor="nw", height=600, width=600, window=frame2)

for i in range(0, 50):
    for j in range(0, 4):
        frame2.columnconfigure(j, minsize=120)
        Label(frame2, bg="powder blue", text="hello"+str(i)+str(j)).grid(row=i, column=j)

canvas.config(scrollregion=frame2.bbox("all"))

app = root
root.mainloop()

0 个答案:

没有答案