Python/Tkinter 图像和文本标签碰撞

发布于 2024-12-18 16:19:18 字数 1171 浏览 0 评论 0原文

我最近一直在研究 Python,想要制作一个可以从 CSV 读取并正确显示它的 GUI。

CSV 构建:

name,description,image location
steven,some guy,/res/pic/steven.gif

前两个条目应放入文本标签中,最后一个条目应用作图像。 在我的代码中,我插入了图片,效果很好。但一旦我也嵌入了文本标签,我认为应用程序就会陷入无限循环。 如果我从代码中删除图像,文本标签就会起作用,反之亦然。

from Tkinter import *
from PIL import *
import os
import csv

#Functions 

def insertImage(guiName,picture,x,y):

    #This is the Image label insertion, delete it and Text label works
    img = PhotoImage(file=entryList[picture][2])
    preview = Label(guiName, image=img)
    preview.img = img
    preview.grid(row=x,column=y)

    #This is the Text label insertion, delete it and Image Label works        
    Name = StringVar()
    labelName = Label(mainGUI, textvariable=Name, justify=LEFT)
    Name.set(entryList[picture][2])
    labelName.pack()

global mainGUI
mainGUI = Tk()
mainGUI.geometry("500x500")
mainGUI.title('Index')

reader = csv.reader(open("res/test.csv", "rb"))
entryList = []
for row in reader:
    entryList.append( row )

#insertImage(mainGUI,entryList[1][2],1,1)
insertImage(mainGUI,1,1,1)
#insertImage(mainGUI,2,2,1)

mainGUI.mainloop()

有谁知道问题可能是什么?

I've been tinkering around with Python lately and wanted to make a GUI that reads from a CSV and displays it correctly.

CSV build up:

name,description,image location
steven,some guy,/res/pic/steven.gif

the first two entries should be put in text labels, and the last entry should be used as an image.
In my code I got as far as inserting the picture, which worked. But as soon as I also embedded the text label, I think the application runs into an infinity loop.
If I delete the Image from the code, the text label works and vice versa.

from Tkinter import *
from PIL import *
import os
import csv

#Functions 

def insertImage(guiName,picture,x,y):

    #This is the Image label insertion, delete it and Text label works
    img = PhotoImage(file=entryList[picture][2])
    preview = Label(guiName, image=img)
    preview.img = img
    preview.grid(row=x,column=y)

    #This is the Text label insertion, delete it and Image Label works        
    Name = StringVar()
    labelName = Label(mainGUI, textvariable=Name, justify=LEFT)
    Name.set(entryList[picture][2])
    labelName.pack()

global mainGUI
mainGUI = Tk()
mainGUI.geometry("500x500")
mainGUI.title('Index')

reader = csv.reader(open("res/test.csv", "rb"))
entryList = []
for row in reader:
    entryList.append( row )

#insertImage(mainGUI,entryList[1][2],1,1)
insertImage(mainGUI,1,1,1)
#insertImage(mainGUI,2,2,1)

mainGUI.mainloop()

Does anyone have an idea what the problem might be?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

想挽留 2024-12-25 16:19:18

问题是您正在使用 grid()pack() 将小部件放置在同一个主小部件 (mainGUI) 中。这是行不通的,因为默认情况下,这两个几何管理器都会尝试管理父窗口小部件的大小,最终会争夺大小(这会阻止 GUI 作为副作用出现)。

如果您尝试执行此操作(最后!),最新版本的 Tk(Tkinter 下的库)将抛出错误,但最好的选择是每个小部件仅使用一个几何管理器。 (禁用几何传播有一些微妙之处,可以使这项工作正常进行,并且“父级”在某些情况下可能会有点棘手,但关键问题是您首先就做错了事情。)

此外,单个标签可以包含图像和一些文本;请参阅 compound 选项(该选项启用此功能并控制相对放置规则)。

The problem is that you are using grid() and pack() to position widgets within the same master widget (mainGUI). That won't work, because by default both of those geometry managers attempt to manage the size of the parent widget and end up fighting over the size (which blocks the GUI from ever appearing as a side effect).

The very latest version of Tk (the lib underneath Tkinter) will throw an error if you try to do this (finally!) but your best bet is to just use one geometry manager per parent widget. (There are some subtleties with disabling geometry propagation which can make this work, and “parent” can be a touch tricky in a few situations, but the key issue is that you're doing the wrong thing in the first place.)

Also, a single label can contain both an image and some text; see the compound option (which enables this and controls the relative placement rules).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文