python简单可视化聊天界面_如何用Python制作可视化输入界面
繼續研究Python的應用,我們在有些程序中需要輸入一些參數,可由幾種方式實現
1.直接寫在程序里,適合編程使用
2.使用input()函數,運行程序時輸入
3.做成可視化界面,然后讓程序獲得
今天主要嘗試第三種方法,通過搜索發現tkinter是Python下面向tk的圖形界面接口庫,可以方便地進行圖形界面設計和交互操作編程。tkinter的優點是簡單易用、與Python的結合度好。tkinter在Python 3.x下默認集成,不需要額外的安裝操作,所以先用這個庫上手。
比如在如何用Python自動獲取加密貨幣恐慌指數并提醒這篇文章中,當我獲取了恐慌指數的值時,有兩個參數需要輸入,一個是超過多少之后提醒,比如超過80就是大牛市了;還有一個是小于多少提醒,這個時候是幣價嚴重低估,比如小于10;我想把這兩個參數用可視化界面輸入,該怎么用呢?
from tkinter import * # 導入庫
root = Tk()# 建立tkinter窗口
root.title("恐慌指數提醒參數")# 設置標題
# 設置標簽
Label(root, text='超過多少提醒:').grid(row=0, column=0)# 選項row代表行,column代表列
Label(root, text='小于多少提醒:').grid(row=1, column=0)
# 輸入框
e1 = Entry(root)
e2 = Entry(root)
# tkinter提供了三種布局組件的方式,第一種是pack(),第二種是Grid()網格,第三種是prase()
# Grid允許我們使用表格的形式管理組件
e1.grid(row=0, column=1, padx=10, pady=5)
e2.grid(row=1, column=1, padx=10, pady=5)
Button(root, text='獲取參數并繼續', width=10, command=root.quit) \
.grid(row=3, column=1, sticky=E, padx=10, pady=5)# 退出直接調用根窗口的quit方法
mainloop()
remind_high =float(e1.get())
remind_low =float(e2.get())
print(remind_high)
print(remind_low)
運行之后如下圖,當我們點擊按鈕“獲取參數并繼續時”,輸入的兩個值就會傳遞給remind_high和remind_low。
這時,我們就可以結合如何用Python自動獲取加密貨幣恐慌指數并提醒文章中的程序,把兩個參數換成remind_high和remind_low繼續運行就可以了,接下來的程序如下:
import json
import requests
# 異常監控用
def send_dingding_msg1(content, robot_id='釘釘機器人ID'):
try:
msg = {
"msgtype": "text",
"text": {"content": content + '\n' + datetime.datetime.now().strftime("%m-%d %H:%M:%S")}
}
headers = {"Content-Type": "application/json ;charset=utf-8 "}
url = 'https://oapi.dingtalk.com/robot/send?access_token=' + robot_id
body = json.dumps(msg)
status = requests.post(url, data=body, headers=headers)
if status.status_code == 200:
return status.json()
return status
except Exception as err:
print('釘釘發送失敗', err)
while True:
try:
url ="https://api.alternative.me/fng/?limit=0&format=json&date_format=cn"
response = requests.get(url)
if response.text:# 發現有時候會出現錯誤導致返回數據為空,加此目的當數據為空時繼續獲取余額
FGI =float(response.json()['data'][0]['value'])# 值
print('FGI', FGI)
value_classification = response.json()['data'][0]['value_classification']# 級別
print('value_classification:', value_classification)
timestamp = response.json()['data'][0]['timestamp']# 時間
print('timestamp', timestamp)
time.sleep(2)
# break
else:
time.sleep(2)
continue
if FGI
print('FGI', FGI)
content ='恐慌指數小于指定值,為'+str(FGI)#
send_msg1 = send_dingding_msg1(content)
print(send_msg1)
break
if FGI >remind_high:# 當大于指定值時實現釘釘提醒
print('FGI', FGI)
content ='恐慌指數大于指定值,為'+str(FGI)#
send_msg1 = send_dingding_msg1(content)
print(send_msg1)
except Exception as order_err:
print("查詢出錯,繼續嘗試", order_err)
time.sleep(3)
總結
以上是生活随笔為你收集整理的python简单可视化聊天界面_如何用Python制作可视化输入界面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: sql int 比较_高质量SQL的30
- 下一篇: python正则匹配数字_python