2013-04-02 12 views
5

Sto cercando di visualizzare il testo sopra la mia immagine ma non posso farlo, qualcuno può aiutare per favore.Non riesco a visualizzare il testo sulla mia immagine tkinter

Codice:

# 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 

risposta

7

Il costruttore Label prende un parametro compound. Passare il costruttore sia l'immagine che il testo e passare in compound come Tkinter.CENTER per sovrapporre il testo sull'immagine. La documentazione per questa funzione è a 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 

Si noti inoltre, non dovresti mischiare confezione e la griglia. Dovresti scegliere l'uno o l'altro. Riferimento: http://effbot.org/tkinterbook/grid.htm

P.S. nel caso in cui intendessi che il testo sia verticalmente più alto dell'immagine, puoi usare lo stesso codice di sopra, ad eccezione del set compound=Tkinter.BOTTOM.

Problemi correlati