tkinter实现带背景图片的登录窗口

实现功能:打开系统登录窗口,输入用户名密码,点击登录后跳转到程序主界面,用户名密码在程序代码里,注意运行时需要自己准备一张背景图片back.png。 

tkinter实现带背景图片的登录窗口_第1张图片

主要代码:

self.backCanvas = Canvas(self.frame,width=self.WIDTH,height=self.HEIGHT)
self.backPhoto = ImageTk.PhotoImage(Image.open("back.png").resize((self.WIDTH,self.HEIGHT),                                                                 Image.Resampling.NEAREST))
self.backCanvas.create_image(self.WIDTH//2,self.HEIGHT//2,image=self.backPhoto)
self.backCanvas.pack()#grid(row=0,column=0,rowspan=3,columnspan=2)

 思路为先在登录窗口生成一个画布,然后生成背景图片,将图片铺满画布填充,最后将画布载入。

完整代码如下:

# _*_ coding:utf-8 _*_
"""
@ 功能 : 登录窗口跳转
@ author : sxy
@ create date : 2022/12/06
"""

from tkinter import *
from PIL import Image,ImageTk

# -------------------------------------------------------------------------
#程序登录界面
class LoginFrame(object):
    def __init__(self, master=None):
        self.root = master
        self.root.title("XXXX软件登录")
        #self.root.geometry('400x250+500+500')
        self.WIDTH = 680
        self.HEIGHT = 450
        self.frame = Frame(self.root)
        self.frame.pack()

      
        # Place GUI on the center of screen
        self.ws = self.root.winfo_screenwidth()
        self.hs = self.root.winfo_screenheight()
        x = (self.ws / 2) - (self.WIDTH / 2)
        y = (self.hs / 2) - (self.HEIGHT / 2)-10
        self.root.geometry('%dx%d+%d+%d' % (self.WIDTH, self.HEIGHT, x, y))


        self.backCanvas = Canvas(self.frame,width=self.WIDTH,height=self.HEIGHT)
        self.backPhoto = ImageTk.PhotoImage(Image.open("back.png").resize((self.WIDTH,self.HEIGHT),
                                                                          Image.Resampling.NEAREST))
        self.backCanvas.create_image(self.WIDTH//2,self.HEIGHT//2,image=self.backPhoto)
        self.backCanvas.pack()#grid(row=0,column=0,rowspan=3,columnspan=2)

        self.lableFrameName=Label(self.frame,text='XXXX软件—系统登录',
                                  font=('微软雅黑', 20))
        self.lableFrameName.place(x=200,y=30)

        self.labelUserName=Label(self.frame,text='用户名:',font=('微软雅黑', 11))         
        self.labelUserName.place(x=290,y=140,anchor='e')#grid(row=0,column=0,pady=20)
        self.varUserName=StringVar(self.frame,value='')
        self.entryUserName=Entry(self.frame,font=('微软雅黑', 12),width=15,textvariable=self.varUserName)
        self.entryUserName.place(x=300,y=140,anchor='w')#grid(row=0,column=1,pady=20)
        self.entryUserName.focus_set()  #获得焦点
        
        self.labelPwd=Label(self.frame,text='密码:',font=('微软雅黑', 11))
                       #justify=RIGHT,anchor='w',width=80)
        self.labelPwd.place(x=290,y=180,anchor='e')#grid(column=0,row=1,pady=10)
        self.varPwd=StringVar(self.frame,value='')
        self.entryPwd=Entry(self.frame,show='*',font=('微软雅黑', 11),width=15,textvariable=self.varPwd)
        self.entryPwd.place(x=300,y=180,anchor='w')#grid(column=1,row=1,pady=10)
        
        
        self.buttonOk=Button(self.frame,text='登录',font=('微软雅黑', 12),width=8,
                             command=self.login)     
        self.buttonOk.place(x=210,y=240)#grid(row=2,column=0,padx=20,pady=20)

        self.buttonCancel=Button(self.frame,text='删除',font=('微软雅黑', 12),width=8,
                                 command=self.cancel)
        self.buttonCancel.place(x=350,y=240)#grid(row=2,column=1,padx=10,pady=20)

        
    #---------------------------------------------------------------
    #登录函数
    def login(self):
        name=self.entryUserName.get()
        pwd=self.entryPwd.get()
        if name=='admin' and pwd=='123456':
            self.frame.destroy()           
            #加载主窗口
            MainFrame(self.root)
        else:
            messagebox.showerror('警告',message='用户名或密码错误')
    #---------------------------------------------------------------
    #取消函数
    def cancel(self):
        varUserName.set('')
        varPwd.set('')
        
# ----------------------------------------------------------------------            
#程序主界面
class MainFrame():
    def __init__(self,master=None):
        self.root=master
      
        self.root.title('XXXX数据分析软件')
        self.WIDTH=1000
        self.HEIGHT=760
         # Place GUI on the center of screen
        self.ws = self.root.winfo_screenwidth()
        self.hs = self.root.winfo_screenheight()
        x = int((self.ws / 2) - (self.WIDTH / 2))
        y = int((self.hs / 2) - (self.HEIGHT / 2))
        self.root.geometry('{0:d}x{1:d}+{2:d}+{3:d}'.format(self.WIDTH, self.HEIGHT, x, y))
        self.root.resizable(False,False)
        
        Label(self.root, text=' 这是项目主界面窗口',font=('微软雅黑', 16))\
                      .pack()
      
#---------------------------------------------------------------------------------------------    
if __name__ == "__main__":
    root = Tk()
    app = LoginFrame(root)   
    root.mainloop()
    


你可能感兴趣的:(tkinter应用系统开发,Python,python)