![](/img/trans.png)
[英]Python/Tkinter: Customize horizontal padding between image and text in Label widget?
[英]Python/Tkinter Image and Text label collision
我最近一直在修改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()
有誰知道可能是什么問題?
問題在於您正在使用grid()
和pack()
在同一主窗口小部件( mainGUI
)中定位窗口小部件。 這是行不通的,因為默認情況下,這兩個幾何管理器都試圖管理父窗口小部件的大小並最終爭奪該大小(這阻止了GUI出現副作用)。
如果嘗試這樣做(最后!),Tk的最新版本(Tkinter下的lib)將拋出錯誤,但是最好的選擇是每個父窗口小部件僅使用一個幾何管理器。 (禁用幾何體傳播有一些微妙之處,可以使這項工作有效,在某些情況下,“父級”可能會有些棘手,但關鍵的問題是,一開始您做錯了事情。)
同樣,一個標簽可以同時包含圖像和一些文本。 請參閱compound
選項(啟用該選項並控制相對放置規則)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.