用python做一个性格分析工具【GUI编程实例】

Crossin的编程教室

共 3352字,需浏览 7分钟

 ·

2021-01-23 00:14



九型人格

九型人格是一个近年来倍受国际著名大学MBA学员推崇的课程之一,近十几年来已风行欧美学术界及工商界。
全球500强企业的管理阶层均有研习九型性格,并以此培训员工,建立团队,提高执行力。在当代,它对于企业的前期规划、战略确定、教练指导、企业培训等方面,九型人格有很大的优势。
九型人格不仅仅是一种精妙的性格分析工具,更主要的是为个人修养、自我提升和历练提供更深入的洞察力。

大家好,欢迎来到 Crossin的编程教室 !

很多大公司在招聘和组建团队时很看重“性格分析”,而“九型人格”就是一个被用得很多的性格分析工具,其将性格心理与行为划分为九类,并对应每一类人最适合岗位与职责。咱们先不讨论这个工具是否真的有效和合理。既然企业和领导们会用它,甚至关系到聘用和升职与否,那我们自然也得了解一下,知己知彼方能百战不殆嘛。

今天,我们就用Python开发一套带界面的九型人格性格分析工具。提升编程技术的同时,也可以顺便了解下自己的性格分类!

既然是九型人格分析,首先我们需要拿到它的测试题。翻了很久,在百度文库上找到一份测试原题:
https://wenku.baidu.com/view/19455024dd36a32d72758105.html

测试题总共36道,通过各场景下的行为表现,最终分析出你最接近的人格分类。现在题有了,如何做出测试题呢?我选择使用Python的tkinter模块,将测试题开发为一个可执行的exe工具,说干就干!

基础准备

为了能将代码打包成单独的可执行文件,我们需要先准备测试题与对应的答案,然后提前存储在代码中。我们需要进行相关拆分,这种苦力活就交给拥有雷锋精神的我来完成吧:


界面开发

界面无需太过复杂,提供说明、题目、选项作答、题目切换与操作按钮即可。当然,交卷后,需要显示用户的测试结果,那么开始吧!

tkinter是python自带的GUI模块,无需另行安装。对于像这种简单交互界面的小程序非常适合。并且,因为不用额外的库,在后面打包exe的时候会少踩一些坑。

30 minutes later……

完成!

Main.py

 1from Enneagram_GUI import *
2from tkinter import *
3
4
5def center_window(root, width, height):
6    screenwidth = root.winfo_screenwidth()
7    screenheight = root.winfo_screenheight()
8    size = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
9    root.geometry(size)
10
11
12root = Tk()
13
14center_window(root, 750700)
15
16root.resizable(width=False, height=False)
17root.title('九型人格测试 | 公众号: 清风Python')
18ExamPage(root)
19root.mainloop()

Enneagram_GUI.py

  1# -*- coding: utf-8 -*-
2# @Author   : 王翔
3# @微信号   : King_Uranus
4# @公众号    : 清风Python
5# @GitHub   : https://github.com/BreezePython
6# @Date     : 2019/11/12 23:12
7# @Software : PyCharm
8# @version  :Python 3.7.3
9# @File     : Enneagram_GUI.py
10
11
12# coding:utf-8
13from tkinter import *
14import Enneagram_Exam
15import Enneagram_Result
16import tkinter.messagebox
17
18# 自测说明
19Standard = '此份问卷共有36道测试题目,请在每题中选择你认为最恰当或者最接近描述自己的性格行为的句子,\n' \
20           '请全部作答,最高分的项目很可能成为你的基本性格型态。'
21
22# 人格类型矩阵
23Style_Dict = [
24    {3262102152191222282322},
25    {1161121172201231291331},
26    {4171101142232262301341},
27    {2182122161212241281342},
28    {1242131162192251311361},
29    {5192141181211252292321},
30    {2272112182221272332362},
31    {3191132171242271202352}
32]
33
34
35class ExamPage:
36    def __init__(self, master=None):
37        self.root = master
38        # 用户结果集
39        self.user_result = {}
40        self.status = 1
41        self.All_Exam = Enneagram_Exam
42        self.normal_choice = IntVar()
43        self.start_exam()
44
45    # 上一题方法
46    def before(self):
47        if self.normal_choice.get() != 0:
48            self.user_result[self.status] = self.normal_choice.get()
49            if self.status > 1:
50                self.status -= 1
51                self.body.grid_forget()
52                self.main_exam()
53        else:
54            tkinter.messagebox.showwarning("提示:", message="请先选择答案!")
55
56    # 下一题方法
57    def after(self):
58        if self.normal_choice.get() != 0:
59            self.user_result[self.status] = self.normal_choice.get()
60            if self.status < len(Enneagram_Exam.Exam):
61                self.status += 1
62                self.body.grid_forget()
63                self.main_exam()
64        else:
65            tkinter.messagebox.showwarning("提示:", message="请先选择答案!")
66
67    # 获取当前题目
68    def exam_files(self, num):
69        return list(map(lambda x: x.split('.'), self.All_Exam.Exam[num - 1].strip().split('\n')))
70
71    # 交卷
72    def hand_paper(self):
73        self.user_result[self.status] = self.normal_choice.get()
74        if len(self.user_result) != 36:
75            tkinter.messagebox.showwarning("提示:", message="您还有未完成的测试题!")
76        else:
77            self.exam_result = LabelFrame(self.root, text="测试结果", padx=10, pady=10, fg="red", font=("黑体"'11'))
78            self.exam_result.grid(padx=10, pady=5, sticky=NSEW)
79            sc = Scrollbar(self.exam_result)
80            sc.grid(row=0, column=1, sticky=NS)
81            result_info = Text(self.exam_result, font=("黑体"'11'), width=85, yscrollcommand=sc.set)
82            result_info.grid(row=0, column=0, sticky=NSEW)
83            sc.config(command=result_info.yview)
84            all_num = []
85            for style in Style_Dict:
86                calc_num = list(
87                    point for point in self.user_result if point in style and self.user_result[point] == style[point])
88                if calc_num == None:
89                    all_num.append(0)
90                else:
91                    all_num.append(len(calc_num))
92            user_type = all_num.index(max(all_num))
93            for line in Enneagram_Result.Result[user_type]:
94                result_info.insert(END, line)
95
96    # 启动测试所需控制按钮
97    def start_exam(self):
98        self.title = LabelFrame(self.root, text="自测说明", padx=10, pady=10, fg="red", font=("黑体"'11'))
99        self.title.grid(padx=10, pady=5)
100        note = Label(self.title, text=Standard, justify=LEFT, font=("黑体"'11'))
101        note.grid()
102        self.show = LabelFrame(self.root, text="选项", padx=10, pady=10, fg="red", font=("黑体"'11'))
103        self.show.grid(padx=10, pady=5, sticky=EW)
104        go_back = Button(self.show, text="上一题", width=8, command=lambda: self.before())
105        go_back.grid(row=4, column=0, padx=5, pady=10)
106        to_forword = Button(self.show, text="下一题", width=8, command=lambda: self.after())
107        to_forword.grid(row=4, column=1, padx=5, pady=10, sticky=E)
108        hand_in = Button(self.show, text="交卷", width=8, command=lambda: self.hand_paper())
109        hand_in.grid(row=4, column=2, padx=5, pady=10, sticky=E)
110        exit_sys = Button(self.show, text="退出", width=8, command=lambda: sys.exit())
111        exit_sys.grid(row=4, column=3, padx=5, pady=10, sticky=E)
112        self.main_exam()
113
114    # 测试题主界面
115    def main_exam(self):
116        self.normal_choice.set(0)
117        self.body = LabelFrame(self.root,
118                               text="测试题  第%s题,剩余%s题" % (self.status, (len(Enneagram_Exam.Exam) - self.status)),
119                               padx=10, pady=10, fg="red", font=("黑体"'11'))
120        self.body.grid(padx=10, pady=5, sticky=EW)
121        for option, choice in self.exam_files(self.status):
122            authority_choice = Radiobutton(self.body, text=choice, variable=self.normal_choice, value=option)
123            authority_choice.grid(row=option, sticky=W)
124        Label(self.body, text="  第%s道题,用户选择的结果是:" % self.status, fg="red", font=("黑体"'11')).grid(row=3, column=0,
125                                                                                                   sticky=W)
126        Label(self.body, textvariable=self.normal_choice).grid(row=3, column=0, sticky=E)

剩余的练习题与答案部分,可见完整代码中。

代码完成之后,为了方便给其他人使用,我们需要先将代码打包为exe。

功能OK了,现在不要打扰我,我要做题了!

运行效果:

我的答案是完美型,处女座总是在追求完美的路上跟自己死磕

程序的代码及打包好的exe在这里:

https://pan.baidu.com/s/1tfYDbHgUce0RUJIEkFZcGQ 

提取码:umhr


记得自测完后,告诉我你的所属的类型哦!


作者:王翔
来源:清风Python


_往期文章推荐_

用Python做一个Windows扫雷游戏




浏览 102
点赞
评论
收藏
分享

手机扫一扫分享

举报
评论
图片
表情
推荐
点赞
评论
收藏
分享

手机扫一扫分享

举报