from tkinter import *
import pymysql
# 创建根窗口,并添加组件
root = Tk()
root.title('登录')
root.resizable(0, 0) # 设置窗口大小不可变
canvas = Canvas(root)
canvas.pack(side='top')
# 创建标签
canvas.create_window(100, 50, window=Label(root, font=('宋体', 10), text='用户名', justify='left', padx=5, pady=4))
canvas.create_window(100, 90, window=Label(root, font=('宋体', 10), width=5, text='密码', justify='left', padx=5, pady=4))
# 账号密码输入框
canvas.create_window(210, 50, window=Entry(root, borderwidth=3))
canvas.create_window(210, 90, window=Entry(root, borderwidth=3, show='*'))
canvas.create_window(330, 90, window=Label(root, text='忘记密码', fg='red', font=('宋体', 10)))
# 创建画布背景图
photo = PhotoImage(file='C:\\Users\\asus\\Desktop\\newfile\\faf6102fae04d8ee5a8c5d604d118427.png')
canvas.create_image(200, 150, image=photo)
# 创建登录按钮
canvas.create_window(190, 200, window=Button(root, width=15, bg='#87CEEB', text='立即登录'))
# 进行数据库连接,这里的代码为点击登录后,数据传入数据用,因为本博客主要是讲一下界面心得,所以这里代码被我注释了。
# connection = pymysql.connect('localhost', 'root', '******', "tang_crawler")
# cursor = connection.cursor()
# try:
# cursor.execute('select * from user_information')
# user_information = cursor.fetchall()
# except Exception as e:
# print(e)
# finally:
# if connection:
# cursor.close()
# if cursor:
# connection.close()
#
# button点击事件
def callback():
# user = int(zh_entry.get())
# password = int(mm_entry.get())
# if user == user_information[0][0] and password == user_information[0][1]:
# top = Toplevel()
# top.title('学生管理系统')
# top.geometry('400x400')
# root.state("iconic") # 隐藏窗口,相当于窗口最小化
# else:
pass
mainloop()
运行效果图
说一下,我的背景图是在百度上搜索下载的,选择的是大尺寸的高清图,所以你用其它尺寸可能就不是这样,或者需要调整图片和组件的偏移值。这里的UI优化不怎么好,所以也求大神怎么去优化各组件的样式呢,有没有推荐的Font。