【发布时间】:2019-02-12 11:20:42
【问题描述】:
我正在尝试在我的 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()
【问题讨论】:
-
我可以看看你的完整剧本吗?或者至少有额外的细节,比如
import pil...这也是在def main(self):这样的主要功能中...还有你在任何地方都有tk loop吗? -
@EcSync 抱歉,我发布了更新的代码!
-
您正在使用局部变量
img,该变量在退出函数后将被销毁,因此图像消失了。请改用self.img。 -
@acw1668 我更新了代码抱歉我忘了放类和函数。我尝试将 self 添加到图像中,所以它是 self.img,它就像一个魅力,谢谢!
-
@acw1668 为什么我必须将其引用为 self.img 而不是 img?
标签: python image canvas tkinter