2016-12-12 43 views
0

私は現在、ユーザーに質問するために作成していた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() 

答えて

0

あなたはGUI(relx=1.0, rely=1.0)の右下隅にある画像(anchor='nw')の左上隅を入れています。画像全体が画面外にあります。

これはrelxのドキュメントです。 rely、方向を除いて同一である。

場所ウィンドウの アンカーポイントのマスターウィンドウ内のx座標を指定します。この場合、位置は浮動小数点数として で相対的に指定されます.0.0は、 のマスタの左端に対応し、1.0は マスタの右端に対応します。場所は0.0-1.0の範囲内にある必要はありません。スレーブに-xと -relxの両方が指定されている場合は、その値が合計されます。たとえば、-relx 0.5 -x -2は、スレーブの左端を2ピクセル のマスタの中央の左に配置します。

+0

ああ、ありがとう、それは画面のサイズに相対的な長さと思った! –

+0

@ D.Lee:オブジェクトサイズは、 'relwidth'と' relheight'で指定できます。 –

関連する問題