私は現在、ユーザーに質問するために作成していたGUIの背景に表示する.pngイメージを取得しようとしています。しかし、私の背景は表示されませんでした。私はこれに似た質問を見てきました。イメージをlabel.imageに割り当ててガベージコレクションでは削除されないようにすることを提案しています。しかし、私がこれをしても表示されません。イメージのprintステートメントは "pyimage1"を返します。したがって、プログラムはそれを受け入れますが、役に立たないとわかります。どんな助けでも大歓迎です!TkinterのGUI - PNG背景イメージが表示されませんか?
import tkinter
import os
#Defining main window to work in
root = tkinter.Tk()
root.geometry("1000x500")
root.title("GUI testing program")
questionText = "Question placeholder"
responseText = "Response text placeholder"
#Defining the image to use (got the exact filepath by using os.getcwd())
imgfile = tkinter.PhotoImage(file = (os.getcwd() + '\\test_img.gif'))
#Debugging print, which returns 'pyimage1'
print(imgfile)
#Assigning the image to the background label which will hold the image
backgroundimg = tkinter.Label(root, image = imgfile)
backgroundimg.image = imgfile
backgroundimg.place(x = 0, y = 0, relx = 1.0, rely = 1.0, anchor = 'nw')
#Other items on the screen at the time
questionLabel = tkinter.Label(root, text = questionText, font = ('Verdana',30), bg = '#ffffff', fg = '#000000', relief = 'sunken', justify = 'left', anchor = 'w')
questionLabel.pack(fill = 'x', side = 'top')
inputText = tkinter.Entry(root, text = "Type answer here", font = ('Verdana',30), bg = '#ffffff', fg = '#000000')
inputText.pack(fill = 'x', side = 'top')
submitButton = tkinter.Button(root, text = "Submit", font = ('Verdana',20), bg = '#2c3e50', fg = '#000000', activebackground = '#b3b3b3')
submitButton.pack(side = 'right', anchor = 'n')
responseLabel = tkinter.Label(root, text = responseText, font = ('Verdana',20), bg = '#0000ff', fg = '#ffffff')
responseLabel.pack(side = 'left', anchor = 'n')
root.mainloop()
ああ、ありがとう、それは画面のサイズに相対的な長さと思った! –
@ D.Lee:オブジェクトサイズは、 'relwidth'と' relheight'で指定できます。 –