在顶部tkinter上添加滚动项

时间:2017-08-25 23:31:33

标签: python tkinter tkinter-canvas

我正在尝试做类似新闻的事情,但更简单。只是从顶部填充的项目和旧项目被下推。我正在参考Adding a scrollbar to a group of widgets in Tkinter,但是这个在底部添加了新项目,我无法做到这一点,以便最新的项目总是添加到顶部而旧的项目位于底部。先感谢您。 到目前为止我有这个:

import Tkinter as tk
from Tkinter import *
import PIL
from PIL import Image, ImageTk

root = tk.Tk()

canvas = tk.Canvas(root, borderwidth=0, background="#ffffff")
frame = tk.Frame(canvas, background="#ffffff")
vsb = tk.Scrollbar(root, orient="vertical", command=canvas.yview)
canvas.configure(yscrollcommand=vsb.set)

vsb.pack(side="right", fill="y")
canvas.pack(side="left", fill="both", expand=True)
canvas.create_window((4,4), window=frame, anchor="nw")

row=0

def populate(frame):
    global row
    row += 1

            #image files
    imfile = "potato.jpeg"
    im=Image.open(imfile)
    resized=im.resize((50,50), PIL.Image.ANTIALIAS)
    tkimage = ImageTk.PhotoImage(resized)

    t="the item number %s \t\t" %row 


    tk.Label(frame, text=t, compound=tk.RIGHT).pack()

def onFrameConfigure(canvas):
    '''Reset the scroll region to encompass the inner frame'''
    canvas.configure(scrollregion=canvas.bbox("all"))

def update_status():
    populate(frame)
    root.after(1000, update_status)


root.after(1000, update_status)
frame.bind("<Configure>", lambda event, canvas=canvas: onFrameConfigure(canvas))
root.geometry("500x500")
root.mainloop()

1 个答案:

答案 0 :(得分:0)

谢谢你们,我发现了它:

tk.Label(frame, text=t,image=tkimage, compound=tk.RIGHT).pack(side="bottom")
b=Button(frame, text="see more for %s" %row, command=new_window).pack(side="bottom")