如何制作标签列表的“滚动条”? python tkinter

时间:2019-04-14 12:11:30

标签: python tkinter

这是我的代码:

from tkinter import *
from PIL import Image, ImageTk


im = Image.open(r"1asd.jpg")

root = Tk()
tkimage = ImageTk.PhotoImage(im)
lst = []
for i in range(1, 100):
    A = Label(root, image=tkimage, text="File number "+str(i), compound=RIGHT)
    A.config(font=("Courier", 44))
    lst.append(A)

for i in lst:
    i.pack()
root.mainloop()

问题是,我看不到所有图像,因为它们太多了。如何制作滚动条?我尝试使用“列表框”对象,但不允许我在文本旁边放置图片。

1 个答案:

答案 0 :(得分:0)

尝试做这样的事情

from tkinter import *
from PIL import Image, ImageTk

root = Tk()
ws = root.winfo_screenwidth()
hs = root.winfo_screenheight()
w = 1000
h = 1000
x = (ws / 2) - (w / 2)
y = (hs / 2) - (h / 2)
root.geometry('%dx%d+%d+%d' % (w, h, x, y))
root.update()
canvas = Canvas(root, bg="Black", width=root.winfo_width(), height=root.winfo_height())
canvas.pack()

im = Image.open("home.png")
tkimage = ImageTk.PhotoImage(im)
lst = []
y = 0
for i in range(1, 100):
    label = Label(canvas,image=tkimage, text="File number " + str(i), font=("Courier", 44), compound=RIGHT)
    canvas.create_window(0, y, window=label, anchor=NW)
    y += 60

scrollbar = Scrollbar(canvas, orient=VERTICAL, command=canvas.yview)
scrollbar.place(relx=1, rely=0, relheight=1, anchor=NE)
canvas.config(yscrollcommand=scrollbar.set, scrollregion=(0, 0, 0, y))

root.mainloop()

我用Canvas放置了所有标签并滚动了它们