简体   繁体   English

python tkinter图像显示

[英]python tkinter image display

I tried to display the image with this python code but the image will not display on the gui window. 我试图用此python代码显示图像,但图像不会显示在gui窗口中。 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). 如果你指的这个讨论 ,可以看到有两件事情你的代码错误:没有load_image的方法,而不是你想PhotoImage(file=)其次,你加载一个.jpg这可能是与Tkinter的麻烦(可能不是因为该文章于2008年发布,但如果更改您的功能不起作用,那么接下来要研究的内容)。

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 . 例如,正如布莱恩·奥克利(Bryan Oakley)所指出的那样,直到窗口被销毁, main()才会被调用,因为它是在root.mainloop()之后root.mainloop() ,并且没有Room定义。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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