可滚动图像超出范围

时间:2019-05-21 07:43:06

标签: image python-2.7 tkinter text scrollable

当我滚动文本小部件时,图像越界。有代码可以解决我的问题吗?

我正在使用Tkinter的文本小部件。

def chat():       
        self.t = Text(self.root, borderwidth=4, width=72, height=15, font=("david",14))
        self.t.place(x=10, y=200)
        self.t.config(state=DISABLED)

def add_image(self, path,name):
        image = Image.open(path)
        photoImg = ImageTk.PhotoImage(image.resize((170, 170)))
        icon_size = Label(self.root)
        icon_size.image = photoImg  
        icon_size.configure(image=photoImg)
        icon_size.pack(side=LEFT)
        self.t.config(state=NORMAL)
        self.t.insert(END, name + ": ")
        self.t.window_create(END, window=icon_size) 
        self.t.insert(END, '\n')
        self.t.config(state=DISABLED)

1 个答案:

答案 0 :(得分:0)

这是一个可能有用的示例,但是它基于许多假设,因为您尚未提供完整的代码示例。它也是为Python 3而非2.7编写的,因为Python软件基金会不建议在2.7中编写新代码。

from tkinter import *
import tkinter.scrolledtext as tkst

root = Tk()
t = tkst.ScrolledText(root,borderwidth=4,width=72,height=15)
t.grid()
t.config(state=DISABLED)

names = ['bill','allan','steve','chloe','annabelle','louise','stephanie']
images = []
icons = []

for name in names:
    img = PhotoImage(file='icon.gif')
    icon = Label(root)
    icon.image = img
    icon.configure(image=img)

    t.config(state=NORMAL)
    t.insert(END, name+":")
    t.window_create(END,window=icon)
    t.insert(END, "\n")

    images.append(img)
    icons.append(icon)

root.mainloop()

请注意,我将图像存储在列表中,以便保留它们。我还使用了tkinter滚动文本小部件向文本小部件添加了滚动条。