簡體   English   中英

如何讓我的 Label Tkinter 元素顯示?

[英]How do I make my Label Tkinter element display?

目前,我正在創建一個文字冒險游戲。 現在為了使它比使用 python 控制台的許多人更好一點,我正在使用 Tkinter 來制作 GUI。 只有它顯示我的開始屏幕的背景圖像,而不是我放在那里的文本! 請幫忙!

# writes the title as text in the window
titleFont = tkFont.Font(family = "Comic Sans MS", size = 20)
titleText = tkinter.Label(app, text="Gods of This World", font=titleFont)
titleText.pack()

# sets the background image to the games 3 colors (Red, Green, Grey)
C = tkinter.Canvas(app, height=300, width=250)
filename = tkinter.PhotoImage(file = "C:/Users/" + getpass.getuser() + "/Desktop/Gods of This World/net/godsofthisworld/ui/images/backgroundimage.png")
background_label = tkinter.Label(app, image=filename)
background_label.place(x=0, y=0, relwidth=1, relheight=1)
C.pack()

您將圖像放在文本之后 - 並使用.place(x=0, y=0, relwidth=1, relheight=1) - 因此文本隱藏在圖像后面。

把它放在不同的順序 - 第一個圖像,下一個文本。

import tkinter as tk

app = tk.Tk()

# first image   
photo = tk.PhotoImage(file="image.png")
background_label = tk.Label(app, image=photo)
background_label.place(x=0, y=0, relwidth=1, relheight=1)

# next text   
titleText = tk.Label(app, text="Gods of This World")
titleText.pack()

app.mainloop()

順便說一句:帶有文本的label將具有灰色背景 - 您無法將其刪除。 如果您想要沒有背景的文本,請使用Canvascanvas.create_image()canvas.create_text() - 沒有pack()place()

import tkinter as tk

WIDTH = 800
HEIGHT = 600

app = tk.Tk()

canvas = tk.Canvas(width=WIDTH, height=HEIGHT)
canvas.pack()

photo = tk.PhotoImage(file="image.png")
canvas.create_image((WIDTH/2, HEIGHT/2), image=photo) #, anchor='center')
canvas.create_text((WIDTH/2, HEIGHT/2), text="Gods of This World") #, anchor='center')

app.mainloop()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM