簡體   English   中英

我無法在我的tkinter圖像上顯示文字

[英]I can't display text over my tkinter image

我試圖在我的圖像上顯示文字,但我不能這樣做,任何人都可以幫助請。

碼:

# import Image and the graphics package Tkinter
import Tkinter
import Image, ImageTk

class simpleapp_tk(Tkinter.Tk):
    def __init__(self,parent):
        Tkinter.Tk.__init__(self,parent)
        self.parent = parent
        self.initialize()

    def initialize(self):
##    def create_widgets(self):
        # create welcome label
        label1 = Tkinter.Label(self, text = "Update User")
        label1.grid(row = 0, column = 1, columnspan = 2, sticky = 'W')

# open a SPIDER image and convert to byte format
im = Image.open('C:\Users\JOHN\Desktop\key.jpg')

root = Tkinter.Tk()  # A root window for displaying objects

 # Convert the Image object into a TkPhoto object
tkimage = ImageTk.PhotoImage(im)

Tkinter.Label(root, image=tkimage).pack() # Put it in the display window

root.mainloop() # Start the GUI

Label構造函數采用參數compound 將構造函數傳遞給圖像和文本,並將compound傳遞為Tkinter.CENTER以將文本重疊到圖像上。 有關此功能的文檔,請訪問http://effbot.org/tkinterbook/label.htm

import Tkinter
import Image, ImageTk

# open a SPIDER image and convert to byte format    
im = Image.open(r'C:\Users\JOHN\Desktop\key.jpg')

root = Tkinter.Tk()  # A root window for displaying objects

# Convert the Image object into a TkPhoto object
tkimage = ImageTk.PhotoImage(im)

Tkinter.Label(root, image=tkimage, text="Update User", compound=Tkinter.CENTER).pack() # Put it in the display window

root.mainloop() # Start the GUI

另請注意,您不應該混合包和網格。 你應該選擇其中一個。 參考: http//effbot.org/tkinterbook/grid.htm

PS以防萬一你想要文本垂直高於圖像,你可以使用與上面相同的代碼,除了set compound=Tkinter.BOTTOM

暫無
暫無

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

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