简体   繁体   中英

python tkinter image display

I tried to display the image with this python code but the image will not display on the gui window. The code is here

#!C:\Python27\python.exe
from Tkinter import *

# create the window
root = Tk()
root.title("GUI program")
root.geometry("640x510")

# create a frame in the window to hold widgets
app = Frame(root)
app.grid()

# create a label in the frame
lbl = Label(app, text = "Hi, my name is Greer!")
lbl.grid()

# kick off the windows loop
root.mainloop()

# load background image
def main():
    room_image = load_image("C:\Users\abc\Desktop\Python\model.jpg")
    background = room_image
    the_room = Room(image = room_image,
    screen_width = 640,
    screen_height = 510,
    fps = 50)
    add(the_room)
main()

If you refer to this discussion , you can see there are two things wrong with your code: There is no load_image method, instead you want PhotoImage(file=) , secondly, you're loading a .jpg which could be trouble with tkinter (it might not be since the article was published in 2008, but in case changing your function doesn't work that would be the next thing to look into).

Also, there are some other errors. For example, as Bryan Oakley has pointed out, main() will never get called until the window is destroyed, since it is called after root.mainloop() , and no definition for Room .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM