为什么我的窗口不能加载我下面这段代码中的图片(图片名称和格式都没错,也和代码在同一级文件夹内,程序不报错,能显示窗口,但是不能显示图片):
self.label4=tk.Label(image=tk.PhotoImage(file='图标.png')) //该段代码在34行
self.label4.place(x=50,y=50)
全代码:
import pymysql
import tkinter as tk
from PIL import ImageTk,Image
#连接数据库
def sql_link():
conn=pymysql.connect(host='localhost',user='root',port=3306,db='testdb',charset='utf8',passwd='021018zxh')
cur=conn.cursor()#生成游标对象
sql="select* from employees"#sql语句
cur.execute(sql)#执行sql语句
data=cur.fetchall()#通过fetchall方法获得数据
for i in data:
print(i)
cur.close()
conn.close()
class MainWindow(tk.Tk):
def __init__(self):
super(MainWindow, self).__init__()
self.title('我来上厕所')
self.resizable(False,False)
self.geometry('500x300+400+200')
self.iconphoto(True,tk.PhotoImage(file='图标2.png'))
self.loadIt()
#登录界面
def loadIt(self):
self.label1=tk.Label(text='用户登录')
self.label1.place(x=220,y=20)
self.label2=tk.Label(text='账户:')
self.label2.place(x=130,y=80)
self.label3=tk.Label(text='密码:')
self.label3.place(x=130,y=120)
self.label4=tk.Label(image=tk.PhotoImage(file='图标.png'))
self.label4.place(x=50,y=50)
self.button1=tk.Button(text='管理员登录?点这试试',font=('宋体',8),fg='#A4A4A4',bd=0,command=None)
self.button1.place(x=200,y=280)
self.button2 = tk.Button(text='注册账户', font=('黑体', 8), fg='#A4A4A4', bd=0, command=self.load_register)
self.button2.place(x=180, y=150)
self.button3 = tk.Button(text='忘记密码', font=('黑体', 8), fg='#A4A4A4', bd=0, command=self.Retrieve_password)
self.button3.place(x=280, y=150)
self.button4 = tk.Button(text='登录', width=8,height=1,command=None)
self.button4.place(x=220, y=200)
self.txt1 = tk.StringVar()
self.stxt1 = tk.Entry(self, textvariable=self.txt1)
self.stxt1.place(x=180, y=80, width=170, height=20)
self.txt2 = tk.StringVar()
self.stxt2 = tk.Entry(self, textvariable=self.txt2,show='●')
self.stxt2.place(x=180, y=120, width=170, height=20)
#注册页面
def load_register(self):
window_sign_up=tk.Tk()
window_sign_up.geometry('500x300+400+200')
window_sign_up.wm_title('新用户')
window_sign_up.resizable(False,False)
window_sign_up.Rlabel1=tk.Label(window_sign_up,text='欢迎注册《我来上厕所》', font=('黑体', 12)).place(x=160, y=20)
window_sign_up.Rlabel2=tk.Label(window_sign_up,text='给您一流的如厕体验~', font=('宋体', 8)).place(x=220, y=50)
window_sign_up.Rlabel3=tk.Label(window_sign_up,text='设置昵称:').place(x=110, y=80)
window_sign_up.Rlabel4=tk.Label(window_sign_up,text='设置密码:').place(x=110, y=120)
window_sign_up.Rlabel5=tk.Label(window_sign_up,text='验证密码:').place(x=110, y=160)
window_sign_up.Rtxt1 = tk.StringVar()
window_sign_up.Rstxt1 = tk.Entry(window_sign_up, textvariable=window_sign_up.Rtxt1).place(x=180, y=80, width=170, height=20) # 昵称输入
window_sign_up.Rtxt2 = tk.StringVar()
window_sign_up.Rstxt2 = tk.Entry(window_sign_up, textvariable=window_sign_up.Rtxt2, show='●').place(x=180, y=120, width=170, height=20) # 密码输入
window_sign_up.Rtxt3 = tk.StringVar()
tk.Entry(window_sign_up, textvariable=window_sign_up.Rtxt3, show='●').place(x=180, y=160, width=170, height=20) # 二次确认密码
window_sign_up.Rbutton = tk.Button(window_sign_up,text='立即注册', width=8, height=1, command=None).place(x=220, y=200)
#忘记密码页面
def Retrieve_password(self):
window_r_p=tk.Tk()
window_r_p.geometry('500x300+400+200')
window_r_p.wm_title('找回密码')
window_r_p.resizable(False, False)
window_r_p.Rlabel1 = tk.Label(window_r_p, text='┃找回密码', font=('黑体', 12)).place(x=100,y=60)
window_r_p.Rlabel2 = tk.Label(window_r_p, text='请输入账号', font=('黑体', 10)).place(x=115,y=100)
window_r_p.Rtxt1 = tk.StringVar()
tk.Entry(window_r_p, textvariable=window_r_p.Rtxt1,background='#E0F2F7',relief="ridge").place(x=120, y=140, width=250, height=35)
window_r_p.Rbutton1=tk.Button(window_r_p,text='立即找回',width=8,height=1,command=None).place(x=220,y=200)
pass
if __name__=='__main__':
mainwin=MainWindow()
mainwin.mainloop()
程序中用到的两张图片如下:
必须在同一个文件夹内,而不是在同一级文件夹内
你先写个open来测试,看到底能不能打开文件,如果能找到文件,再说为什么不显示的问题,一步一步排查原因