【问题标题】:Scrollable image out of bound可滚动图像越界
【发布时间】:2019-05-21 07:43:06
【问题描述】:

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

我正在使用 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)

【问题讨论】:

  • 您能否提供一个完整且可执行的示例来说明您遇到的问题?您共享的代码不够完整,无法让任何人复制您的问题。

标签: image python-2.7 tkinter text scrollable


【解决方案1】:

这是一个可能有用的示例,但它基于许多假设,因为您没有提供完整的代码示例。它也是为 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 滚动文本小部件向文本小部件添加滚动条。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-18
    • 1970-01-01
    • 2020-01-18
    • 1970-01-01
    相关资源
    最近更新 更多