我不熟悉python,特别是GUI问题.
我正在尝试从其他类添加图像,我找到了添加其他对象但不添加图像的方法.
这段代码很好用:
from Tkinter import *
from PIL import Image, ImageTk
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.parent = master
self.initUI()
def initUI(self):
self.outputBox = Text(self.parent, bg='black', height= 10, fg='green', relief=SUNKEN, yscrollcommand='TRUE')
self.outputBox.pack(fill='both', expand=True)
def main():
root = Tk()
app = Application(root)
app.parent.geometry('800x500')
app.parent.configure(background = 'red')
path = "../img/Stalin.jpeg"
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(app.parent, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
app.mainloop()
main()
但是当我尝试从类中添加图像时,它会打开窗口但是没有图像:
from Tkinter import *
from PIL import Image, ImageTk
class Application(Frame):
def __init__(self, master=None):
Frame.__init__(self, master)
self.parent = master
self.initUI()
def initUI(self):
self.outputBox = Text(self.parent, bg='black', height= 10, fg='green', relief=SUNKEN, yscrollcommand='TRUE')
self.outputBox.pack(fill='both', expand=True)
path = "../img/Stalin.jpeg"
img = ImageTk.PhotoImage(Image.open(path))
panel = Label(self.parent, image = img)
panel.pack(side = "bottom", fill = "both", expand = "yes")
def main():
root = Tk()
app = Application(root)
app.parent.geometry('800x500')
app.parent.configure(background = 'red')
app.mainloop()
main()
在eclipse上使用python 2.7.
这篇博客讨论了在Python Tkinter GUI中遇到的问题,即如何正确地在类中添加图像。作者提供了两段代码,一段展示了图像成功显示的场景,另一段则显示了在类内部尝试添加图像但未能显示的情况。问题似乎与图像对象的生命周期和其在类中的使用方式有关。
1004

被折叠的 条评论
为什么被折叠?



