Python可視化界面

可視化界面程序,原本不想寫,只在console臺運行就好,可是後來不少小夥伴都有這樣的需求:python

須要從redis中刪除某個key的value,而後須要跟key去查,有些小夥伴不會用redis,就產生以下的產物。redis

能夠看出main後面的tk對象,Tkinter 是 Python 的標準 GUI 庫。Python 使用 Tkinter 能夠快速的建立 GUI 應用程序。Python3.x 版本使用的庫名爲 tkinter,即首寫字母 T 爲小寫。其餘的沒啥特別的囑咐,導入模塊後,建立控件,指定控件的master,告訴GM(geometry manager)有個控件產生了,這裏就不講控件排版了,我實在審美很差……json

# 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()
相關文章
相關標籤/搜索