Python Tkinter图像未显示在画布上

Python Tkinter图像未显示在画布上,python,image,canvas,tkinter,Python,Image,Canvas,Tkinter,我试图在tkinter gui的画布上绘制图像。但当我绘制文本时,它会显示出来,我想知道为什么它不会显示,以及如何修复它 import tkMessageBox, PIL.ImageTk, PIL.Image, socket, queue, ttk from threading import Thread from Tkinter import * class GUI(Frame): def __init__(self, parent): Frame.__init__

我试图在tkinter gui的画布上绘制图像。但当我绘制文本时,它会显示出来,我想知道为什么它不会显示,以及如何修复它

import tkMessageBox, PIL.ImageTk, PIL.Image, socket, queue, ttk
from threading import Thread
from Tkinter import *


class GUI(Frame):
    def __init__(self, parent):
        Frame.__init__(self,parent)
        self.pack()

def main(self):
self.bBar = Frame(self, bg="#3A3A3C", bd=0)
self.bBar.place(x=0, y=450, width=700, height=600)

self.can = Canvas(self.bBar, width=700, height=50, highlightthickness=0, bg="red", bd=0)
img = PIL.ImageTk.PhotoImage(PIL.Image.open("Images/uBar.png"))
self.can.pack()
self.can.create_text(100, 10, text="My Text")
self.can.create_image(10,10, image=img, anchor=NW)

if "__Main__":
root = Tk()
root.title("Frozen Cloud")
root.geometry("700x500")
root.config(background="White")
root.resizable(0,0)
root.iconbitmap("Images/Icon.ico")

gui = GUI(root)
#gui.splashSc()
gui.mScreen()

root.mainloop()

试试这个,您已经使用了self,但没有在函数中定义,而且
root=tk()
应该在开头

root = Tk()
def main(self):

    self.bBar = Frame(self, bg="#3A3A3C", bd=0)
    self.bBar.place(x=0, y=450, width=700, height=600)

    self.can = Canvas(self.bBar, width=700, height=50, highlightthickness=0, bg="red", bd=0)
    img = PIL.ImageTk.PhotoImage(PIL.Image.open("Images/uBar.png"))
    self.can.pack()
    self.can.create_text(100, 10, text="My Text")
    self.can.create_image(10,10, image=img, anchor=NW)

    if "__Main__":

        root.title("Frozen Cloud")
        root.geometry("700x500")
        root.config(background="White")
        root.resizable(0,0)
        root.iconbitmap("iImages/Icon.co")

        gui = GUI(root)
        #gui.splashSc()
        gui.mScreen()

root.mainloop()

试试这个,您已经使用了self,但没有在函数中定义,而且
root=tk()
应该在开头

root = Tk()
def main(self):

    self.bBar = Frame(self, bg="#3A3A3C", bd=0)
    self.bBar.place(x=0, y=450, width=700, height=600)

    self.can = Canvas(self.bBar, width=700, height=50, highlightthickness=0, bg="red", bd=0)
    img = PIL.ImageTk.PhotoImage(PIL.Image.open("Images/uBar.png"))
    self.can.pack()
    self.can.create_text(100, 10, text="My Text")
    self.can.create_image(10,10, image=img, anchor=NW)

    if "__Main__":

        root.title("Frozen Cloud")
        root.geometry("700x500")
        root.config(background="White")
        root.resizable(0,0)
        root.iconbitmap("iImages/Icon.co")

        gui = GUI(root)
        #gui.splashSc()
        gui.mScreen()

root.mainloop()

我能看看你的完整剧本吗?或者至少有一些额外的细节,比如
import-pil
…这也是像
def-main(self):
…你是否有
tk循环
在任何地方?@EcSync很抱歉我发布了更新的代码!您正在使用局部变量
img
,该变量在退出函数后将被销毁,因此图像将消失。改用
self.img
。@acw1668我更新了代码,很抱歉我忘了放置类和函数。我试着将self添加到图像中,所以它是self.img,它就像一个符咒,谢谢@acw1668为什么我必须引用它作为self.img而不是img?我可以看看你的完整脚本吗?或者至少有一些额外的细节,比如
import-pil
…这也是像
def-main(self):
…你是否有
tk循环
在任何地方?@EcSync很抱歉我发布了更新的代码!您正在使用局部变量
img
,该变量在退出函数后将被销毁,因此图像将消失。改用
self.img
。@acw1668我更新了代码,很抱歉我忘了放置类和函数。我试着将self添加到图像中,所以它是self.img,它就像一个符咒,谢谢@acw1668为什么我必须引用它作为self.img而不是img?