8

画像の上にテキストを表示しようとしていますが、これはできません。誰か助けてください。

コード:

# 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
4

1 に答える 1

14

Label コンストラクターはパラメーターを取りますcompound。画像とテキストの両方をコンストラクターに渡し、画像にテキストを重ねてcompoundasを渡します。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

また、pack と grid を混在させてはいけません。どちらかを選択する必要があります。参照: http://effbot.org/tkinterbook/grid.htm

PS テキストを画像よりも垂直方向に高くしたい場合に備えて、 set を除いて上記と同じコードを使用できますcompound=Tkinter.BOTTOM

于 2013-04-03T01:07:22.300 に答える