Python可视化界面

可视化界面程序,本来不想写,只在console台运行就好,但是后来很多小伙伴都有这样的需求:

需要从redis中删除某个key的value,然后需要跟key去查,有些小伙伴不会用redis,就产生如下的产物。

可以看出main后面的tk对象,Tkinter 是 Python 的标准 GUI 库。Python 使用 Tkinter 可以快速的创建 GUI 应用程序。Python3.x 版本使用的库名为 tkinter,即首写字母 T 为小写。其他的没啥特别的嘱咐,导入模块后,创建控件,指定控件的master,告诉GM(geometry manager)有个控件产生了,这里就不讲控件排版了,我实在审美不好……

# coding=utf-8
'''
Created on 2018-07-28
updated on 2018-08-06
@author:
Description:封装Redis三条命令,可以清理测试环境短信次数上限

import redis, json
import configparser
from tkinter import *
global phone
'''

def connectRedis():

    confInfo = configparser.ConfigParser()
    confInfo.read('config/config.ini')
    redisHost = confInfo.get('redis_45', 'host')
    # redis基本操作,消耗资源,每链接一次之后就断开了
    # re = redis.Redis(host=redisHost, port=6379, db=4, decode_responses=True),
    pool = redis.ConnectionPool(host=redisHost, port=6379, db=XXX, decode_responses=True)
    re = redis.StrictRedis(connection_pool=pool)
    return re


def checkPhone(phoneNum):
    # 传phone进来
    smsKey = 'uc_user_sms_times_' + str(phoneNum)
    print(smsKey)
    result_re = connectRedis()
    # print()
    try:
        if result_re.get(smsKey) is None:
            print('没有记录,无需清理。')
    except Exception as e:
        print("获取key对应的内容出错!")
        #raise e

    try:
        if result_re.get(smsKey) is not None:
            result = json.loads(result_re.get(smsKey))

            # smsCodeTimes是短信验证码次数
            smsCodeTimes = result['4']['num']
            print(smsCodeTimes)

            #测试环境是10次,线上5次,线上没权限清理,测试环境改了的话就可以改这个参数
            if int(smsCodeTimes) == 10:
                result_re.delete(smsKey)

                # 清除成功
                print('清除成功!')
            else:

                # 不足次数,可继续使用
                print('不足次数,可继续使用!')
    except Exception as e:
        print("出现未知错误!")
        raise e

def buttonBut():

    phoneNum = phone.get()
    checkPhone(phoneNum)


if __name__ == '__main__':

    # 创建窗口对象
    mainWindow = Tk()
    # 设置窗口大小
    mainWindow.geometry('300x200')
    # 禁止拖动窗口
    mainWindow.resizable(0, 0)
    phone = StringVar()
    mainWindow.title("限制解除")
    Label(mainWindow, text="请正确输入要清除限制的手机号:", padx=10).grid(row=0, sticky=W)
    Entry(mainWindow, textvariable=phone).grid(row=1, column=0)
    Button(mainWindow, text="清除次数上限", command=buttonBut).grid(row=2, sticky=SE)
    mainWindow.mainloop()

  

猜你喜欢

转载自www.cnblogs.com/yuki-nana/p/11093046.html