带有 StringVar 的 Tkinter 文件名 [英] Tkinter Filenames with StringVar

查看:50
本文介绍了带有 StringVar 的 Tkinter 文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 tkinter 环境(树莓上的 python 3.2)中设置文件名时遇到问题.为了说明我的意思,我将使用我的代码:

I have a problem setting a filename in a tkinter environment (python 3.2 on a raspberry). To specify what I mean, I will use my code:

from tkinter import Tk, Canvas, StringVar
from PIL import ImageTk, Image
from threading import Thread

class proc(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        self.root=tkinter.Tk()
        self.labelstring = StringVar()
        self.labelstring.set('Foo')

        self.path = StringVar()
        self.path.set('cold.jpg')

        canvas = Canvas(self.root, width=888, height=600)
        canvas.pack()

        im = Image.open(self.path) #<-- does not work
        canvas.image = ImageTk.PhotoImage(im)
        canvas.create_image(0, 0, image=canvas.image, anchor='nw')

        label = tkinter.Label(self.root,textvariable=self.labelstring)
        label.pack()
        self.root.mainloop()


app = proc()
app.start()


for i in range(0, 10):
    time.sleep(5)
    proc.labelstring.set(i)

我更改标签的部分 labelstring.set(i) 工作正常,但不起作用的是通过 path.set('image.jpg')<发送文件名/代码>.我知道,文件类型不是这样的路径,它是 tkinter.StringVar 对象...我没有找到使它成为路径变量的好方法.

The part where I change the label labelstring.set(i) works fine, but what does not work is sending a filename via path.set('image.jpg'). I konw, the filetype is not a path this way, it is a tkinter.StringVar Object... I did not find a good way to make it a path variable.

在一天结束时

im = Image.open(self.path)
canvas.image = ImageTk.PhotoImage(im)
canvas.create_image(0, 0, image=canvas.image, anchor='nw')

不能用先前定义的self.path.set('image.jpg')调用.我想要一个 xy 图片列表并执行 path.set(piclist[i]) 来更改 tkinter.canvas 中的图像.

cannot be called with previously define self.path.set('image.jpg'). I want to maybe have a list of xy pics and do path.set(piclist[i]) to change the image in the tkinter.canvas.

推荐答案

我不知道你想要实现什么,以及为什么在这里使用 Threads.您的代码有一些不一致之处,缺少导入语句等.因此,我简化了它,以便我可以运行它并只专注于您指出的行.简化版是:

I dont know what you want to achieve, and why use Threads here. Your code has some inconsistencies, missing import statements, etc. Thus, I simplified it so that I can run it and just concentrate on the line you had indicated. The simplified version is:

from tkinter import Tk, Canvas, StringVar, Label
from PIL import ImageTk, Image
from threading import Thread

class proc():
    def __init__(self):
        pass
       # Thread.__init__(self)

    def run(self):
        self.root=Tk()
        self.labelstring = StringVar()
        self.labelstring.set('Foo')

        self.path = StringVar()
        self.path.set('empty.gif')

        canvas = Canvas(self.root, width=888, height=600)
        canvas.pack()

        im = Image.open(self.path.get()) #<-- does not work
        canvas.image = ImageTk.PhotoImage(im)
        canvas.create_image(0, 0, image=canvas.image, anchor='nw')

        label = Label(self.root,textvariable=self.labelstring)
        label.pack()
        self.root.mainloop()


app = proc()
app.run()

专注于行不通的行,在您的示例中,您有:

Concentrating on the line which does not work, in your example you have:

im = Image.open(self.path)

但是您应该按如下方式获取文件的路径(如我的示例):

But you should be getting the file's path as follows (as in my example):

im = Image.open(self.path.get())

这篇关于带有 StringVar 的 Tkinter 文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆