如何实现python颜值打分小系统

发布时间:2021-09-28 09:49:44 作者:iii
来源:亿速云 阅读:137

这篇文章主要介绍“如何实现python颜值打分小系统”,在日常操作中,相信很多人在如何实现python颜值打分小系统问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”如何实现python颜值打分小系统”的疑惑有所帮助!接下来,请跟着小编一起来学习吧!

正文

本文是基于tkinter做的界面化颜值打分小系统哈,快来测测你的颜值打多少分呀~

环境安装部分:Python3、pycharm2021、以及一些自带的模块。

pip install -i https://pypi.douban.com/simple/ pillow 
pip install -i https://pypi.douban.com/simple/ baidu-aip

首先还是肯定配置百度api参数如下:

APP_ID = '15768642'
API_KEY = 'xhiiGmGPRCRj10XIqVlVeCky'
SECRET_KEY = 'ZDMMAO7StwTKzW8BspVQxvoGtdgSW4yI'
a_face = AipFace(APP_ID, API_KEY, SECRET_KEY)
image_type = 'BASE64'
options = {'face_field': 'age,gender,beauty'}

标题设计颜色、字体等:

def title(self):
        """标题设计"""
        lb = tk.Label(self.root, text='颜值打分系统',
                      bg='#008B8B',
                      fg='lightpink', font=('楷书', 30),
                      width=20,
                      height=2,
                      # relief=tk.SUNKEN
                      )
        lb.place(x=200, y=10)

设置了界面化程序的背景大小等:

class ScoreSystem():
 
    root = tk.Tk()
 
    # 修改程序框的大小
    root.geometry('800x500')
 
    # 添加程序框标题
    root.title('颜值打分系统')
 
    # 修改背景色
    canvas = tk.Canvas(root,
                       width=800,  # 指定Canvas组件的宽度
                       height=500,  # 指定Canvas组件的高度
                       bg='#E6E8FA')  # 指定Canvas组件的背景色
    canvas.pack()

主函数运行:

 def start_interface(self):
        """主运行函数"""
        self.title()
        self.time_component()
 
        # 打开本地文件
        tk.Button(self.root, text='打开文件', command=self.show_original_pic).place(x=50, y=150)
        # 进行颜值评分
        tk.Button(self.root, text='颜值识别', command=self.open_files2).place(x=50, y=230)
 
        # 退出系统
        tk.Button(self.root, text='退出软件', command=self.quit).place(x=50, y=390)
        # 显示图框标题
        tk.Label(self.root, text='原图', font=10).place(x=380, y=120)
        # 修改图片大小
        self.label_img_original = tk.Label(self.root)
        # 设置显示图框背景
        self.cv_orinial = tk.Canvas(self.root, bg='white', width=270, height=270)
        # 设置显示图框边框
        self.cv_orinial.create_rectangle(8, 8, 260, 260, width=1, outline='red')
        # 设置位置
        self.cv_orinial.place(x=265, y=150)
        # 显示图片位置
        self.label_img_original.place(x=265, y=150)
 
        # 设置评分标签
        tk.Label(self.root, text='性别', font=10).place(x=680, y=150)
        self.text1 = tk.Text(self.root, width=10, height=2)
        tk.Label(self.root, text='年龄', font=10).place(x=680, y=250)
        self.text2 = tk.Text(self.root, width=10, height=2)
        tk.Label(self.root, text='评分', font=10).place(x=680, y=350)
        self.text3 = tk.Text(self.root, width=10, height=2)
 
        # 填装文字
        self.text1.place(x=680, y=175)
        self.text2.place(x=680, y=285)
        self.text3.place(x=680, y=385)
 
        # 开启循环
        self.root.mainloop()
 
    def show_original_pic(self):
        """放入文件"""
        self.path_ = askopenfilename(title='选择文件')
        # 处理文件
        img = Image.open(fr'{self.path_}')
        img = img.resize((270, 270), PIL.Image.ANTIALIAS)  # 调整图片大小至270*270
        # 生成tkinter图片对象
        img_jpg_original = ImageTk.PhotoImage(img)
        # 设置图片对象
        self.label_img_original.config(image=img_jpg_original)
        self.label_img_original.image = img_jpg_original
        self.cv_orinial.create_image(5, 5, anchor='nw', image=img_jpg_original)
 
    def open_files2(self):
        # 获取百度API接口获得的年龄、分数、性别
        age, score, gender = face_score(self.path_)
 
        # 清楚text文本框内容并进行插入
        self.text1.delete(1.0, tk.END)
        self.text1.tag_config('red', foreground='RED')
        self.text1.insert(tk.END, gender, 'red')
 
        self.text2.delete(1.0, tk.END)
        self.text2.tag_config('red', foreground='RED')
        self.text2.insert(tk.END, age, 'red')
 
        self.text3.delete(1.0, tk.END)
        self.text3.tag_config('red', foreground='RED')
        self.text3.insert(tk.END, score, 'red')
 
 
 
    def quit(self):
        """退出"""
        self.root.quit()

最后还设置了时间组,随时更新测试颜值的时间,就可以测出不同时间段颜值。

  def get_time(self, lb):
        """获取时间"""
        time_str = time.strftime("%Y-%m-%d %H:%M:%S")  # 获取当前的时间并转化为字符串
        lb.configure(text=time_str)  # 重新设置标签文本
        self.root.after(1000, self.get_time, lb)  # 每隔1s调用函数 get_time自身获取时间
 
    def time_component(self):
        """时间组件"""
        lb = tk.Label(self.root, text='', fg='white', font=("黑体", 15))
        lb.place(relx=0.75, rely=0.90)
        self.get_time(lb)

效果如下:

如何实现python颜值打分小系统

如何实现python颜值打分小系统

如何实现python颜值打分小系统

到此,关于“如何实现python颜值打分小系统”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

推荐阅读:
  1. 什么是流程图?绘制高颜值流程图小技巧?
  2. 使用python实现男神女神颜值打分系统(推荐)

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:SpringBoot跟WebSocket的开发过程是怎样的

下一篇:如何理解springboot2.0.6中META-INF/spring.factories通过系统加载类获取对应的class的全限定名称

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》