简体   繁体   English

Tkinter图像未显示在画布上

[英]Tkinter image not being displayed on canvas

I am trying to draw a image on a canvas in my tkinter gui. 我正在尝试在Tkinter GUI中的画布上绘制图像。 But when I draw text it is displayed I was wondering why it does not display and how I can fix it? 但是当我绘制文本时,我想知道为什么它不显示以及如何解决?

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()

Try this, you have used self but not defined within a function, also root = tk() should be at the start. 尝试此操作,您使用了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()

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

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