'''测试Label组件的基本用法,使用面向对象的方式'''
#coding=utf-8
from tkinter import *
class Application(Frame):
def __init__(self,master=None):
super().__init__(master)
self.master=master
self.pack() #显示
self.creatWidget()
def creatWidget(self):
self.label01=Label(self,text="百战程序员",width=10,height=2,fg="black",bg="white")
self.label01.pack()
self.label02=Label(self,text="小高努力学python",width=20,height=3,fg="blue",bg="white",font=("楷体",30))
self.label02.pack()
global photo #把photos声明成全局变量,如果是局部变量本方法执行完毕后图像对象销毁,窗口显示不出图像
#显示图像
photo=PhotoImage(file="c/mypicture.jpg")
self.label03=Lable(self,image=photo)
self.label03.pack()
if __name__=='__main__':
root=Tk()
root.title("测试Label")
root.geometry("500x200+100+200")
app=Application(master=root)
root.mainloop()
老师,显示图像只能是GIF格式吗,然后只能先在pycharm里打开再写代码才有用吗?我写的是存在桌面上的图片,但是运行错误,这样要怎么才能成功运行呢