Tkinter图像未显示在画布上

时间:2019-02-12 11:20:43

标签: 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__(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()

1 个答案:

答案 0 :(得分:0)

尝试一下,您已使用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()