Python/Tkinter 图像和文本标签碰撞
我最近一直在研究 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您正在使用
grid()
和pack()
将小部件放置在同一个主小部件 (mainGUI
) 中。这是行不通的,因为默认情况下,这两个几何管理器都会尝试管理父窗口小部件的大小,最终会争夺大小(这会阻止 GUI 作为副作用出现)。如果您尝试执行此操作(最后!),最新版本的 Tk(Tkinter 下的库)将抛出错误,但最好的选择是每个父小部件仅使用一个几何管理器。 (禁用几何传播有一些微妙之处,可以使这项工作正常进行,并且“父级”在某些情况下可能会有点棘手,但关键问题是您首先就做错了事情。)
此外,单个标签可以包含图像和一些文本;请参阅
compound
选项(该选项启用此功能并控制相对放置规则)。The problem is that you are using
grid()
andpack()
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).