可視化界面程序,本來不想寫,只在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()