current position:Home>[Python development tool tkinterdiesigner]: example: develop stock monitoring alarm using Tkinter desinger

[Python development tool tkinterdiesigner]: example: develop stock monitoring alarm using Tkinter desinger

2022-01-29 13:52:55 Huoyun cave red boy

【Python Development tool -TKinterDesigner 】 : Let's talk about it with examples : Use TKinterDesigner Develop stock monitoring alarm

TKinterDesigner QQ Group :100180960

This video tutorial is as follows :https://www.bilibili.com/video/BV1LV411r7uc/

Use TkinterDesinger Develop stock monitoring alarm

          Hello everyone , Spent a boring weekend , Many friends are waiting for Monday's A Let's open , Last week's continuous adjustment , So that many people's account profits have shrunk significantly , Even lose money , What should I do ?

          I want to go to , Finally understand , People's emotions are A In front of the stock trend , It's just a piece of shit , Only powerful AI, Scientific analysis , To overcome fear and greed , Reach the other side of the profit .

          Ha ha ha , Get down to business , This section , I teach you to use TKinterDesigner To develop a stock monitor , Help you lock in the bull stock in your heart , Do... At work “ I have a share in my hand , Nothing in my heart !”.

          good , Before we start , Let's prepare today's ingredients :

          open :https://github.com/honghaier-game/TKinterDesigner,  download V1.3.5.

          then , Wash and clean hands , open TKinterDesigner, Create a new empty project .

          Then let's design the function of the product :

            We hope to have a monitored stock pool , The stock code to be entered , The maximum and minimum price are set and put into the stock monitoring pool . After starting monitoring , Refresh per second , If the share price exceeds the ceiling or floor price , Will send SMS notification to our mobile phone number .

          well , In the main form , Drag from the control panel Label,Button,Entry,TreeView, And modify the text and data , The designed interface is as follows :

                  then , We need the view control corresponding to the stock pool TreeView Add a binding variable :StockArray Array . Used to store stock information in the stock pool ( Stock code , Maximum and minimum price ). well , So the interface is ready .

 

            Now let's build a new one Python file , Named StockMonitor.py, Then we encapsulate the main functions we need here .

            One . Stock information acquisition : We use... Here tushare This financial Library , adopt pip install tushare You can install it , Then we can call get_realtime_quotes The interface monitors the stock in real time .

def getStockInfo(stock_code):
    df = ts.get_realtime_quotes(stock_code)
    # e = df[['code','name','time','price']]
    c = df[u'code']
    n = df[u'name']
    t = df[u'time']
    p = df[u'price']
    text = c[0] + " " + n[0] + " " + t[0] + " " + p[0]
    print(text)
    return df

 

            Two . Send SMS service : We use... Here twilio This foreign SMS service , After registration , Get a free trial phone number , Then bind and verify our mobile phone number , After passing, you can get the of the account SID And a TOKEN, stay twilio On our website , Detailed instructions for various development languages , I suggest you visit  https://www.twilio.com/  Try .

        account_sid = ""# Change to apply SID
        auth_token = ""# Change to apply TOKEN
        client = Client(account_sid, auth_token)
        smsText = "Stock:600050 Price 5.55"
        #from_ Change to the trial mobile phone number 
        #to Change to the mobile phone number to receive the message , But remember +86 Oh                  
        client.messages.create(to="+8613488888888",from_="+13343784863",body=smsText)

 

            These two functions , There are many introductions on the Internet , I won't explain them in detail here . With these two important Libraries , In fact, developing this monitoring is very simple .

 

Here are our custom classes StockMonitor.py Code for :

import os
import time
import tushare as ts
import pandas as pd
import threading
from twilio.rest import Client
threadactive = True
def getStockInfo(stock_code):
    df = ts.get_realtime_quotes(stock_code)
    # e = df[['code','name','time','price']]
    c = df[u'code']
    n = df[u'name']
    t = df[u'time']
    p = df[u'price']
    text = c[0] + " " + n[0] + " " + t[0] + " " + p[0]
    print(text)
    return df
class StockMonitor:
    def __init__(self):
        self.StockArray = []
        self.TreeView = None
        self.PhoneNumber = None
        self.Run_Function = None
    def addStock(self,StockCode,MaxPrice,MinPrice):
        self.StockArray.append([StockCode,MaxPrice,MinPrice,False,False])
    def SetMonitorInfo(self,TreeView,PhoneNumber):
        self.TreeView = TreeView
        self.PhoneNumber = PhoneNumber
    def StartMonitoring(self):
        account_sid = ""# Change to apply SID
        auth_token = ""# Change to apply TOKEN
        client = Client(account_sid, auth_token)
        all_items = self.TreeView.get_children()    
        for item in all_items:
            self.TreeView.delete(item)
        for stock_code in self.StockArray:
            df = ts.get_realtime_quotes(stock_code[0])
            e = df[['code','name','price','time']]
            c = df[u'code']
            n = df[u'name']
            p = df[u'price']
            t = df[u'time']
            print(e)
            if float(p[0]) > float(stock_code[2]) :
                self.TreeView.insert('','end',values=(c[0],n[0],t[0],p[0],stock_code[2],stock_code[1]," Over the limit "))
                if stock_code[3] == False:
                    # smsText = " Your stock "+n[0]+"("+c[0]+")  The set ceiling price has been exceeded "+ stock_code[2] + ", Current price "+p[0]+", Please pay attention to !"
                    smsText = "Stock:"+"("+c[0]+") Price:"+p[0]
                    #from_ Change to the trial mobile phone number 
                    client.messages.create(to=self.PhoneNumber,from_="+13343784863",body=smsText)
                    stock_code[3] = True
            elif float(p[0]) < float(stock_code[1]):
                self.TreeView.insert('','end',values=(c[0],n[0],t[0],p[0],stock_code[2],stock_code[1]," Below the lower limit "))
                if stock_code[4] == False:
                    # smsText = " Your stock "+n[0]+"("+c[0]+")  The price is lower than the set lower limit "+ stock_code[1] + ", Current price "+p[0]+", Please pay attention to !"
                    smsText = "Stock:"+"("+c[0]+") Price:"+p[0]
                    print(smsText)
                    #from_ Change to the trial mobile phone number 
                    client.messages.create(to=self.PhoneNumber,from_="+13343784863",body=smsText)
                    stock_code[4] = True
            else:
                self.TreeView.insert('','end',values=(c[0],n[0],t[0],p[0],stock_code[1],stock_code[2]," Smooth fluctuations "))
        self.Run_Function = self.TreeView.after(1000, self.StartMonitoring)
    def StopMonitoring(self):
        self.TreeView.after_cancel(self.Run_Function)
        self.StockArray = []

            We are in this class , adopt addStock Add stock information to the monitoring pool , stay SetMonitorInfo Corresponding to the stock pool TreeView And the phone number of the alarm receiver .

            When we press the start monitoring button on the interface , Call StartMonitoring, At the same time, change the button text to “ Stop monitoring ”, Press again , Then stop monitoring and restore the text to “ Start the monitoring ”.

            With this class code , We go back to TKinterDesigner. We are “ Increase stock ” Right click on the button , eject “ Event response ”, And then to "Command" Add function code :

def Button_4_onCommand(className,widgetName):
    StockCode = Fun.GetUIText(className,"Entry_3")
    StockInfo = StockMonitor.getStockInfo(StockCode)
    StockCode = StockInfo[u'code']
    StockName = StockInfo[u'name']
    StockTime = StockInfo[u'time']
    StockPrice = StockInfo[u'price']
    MaxPrice = Fun.GetUIText(className,"Entry_10")
    MinPrice =Fun.GetUIText(className,"Entry_12")
    TreeView13 = Fun.GetUIEle(className,"TreeView_13")
    TreeView13 .insert('','end',values=(StockCode[0],StockName[0],StockTime[0],StockPrice[0],MinPrice,MaxPrice))
    StockArray = Fun.GetUIData(className,"TreeView_13","StockArray")
    StockArray.append([StockCode[0],MaxPrice,MinPrice])

            This function , The main purpose is to display the filled stock information to TreeView And the bound array variables put into it . Need here import StockMonitor Oh .

          The way to use is “ Start the monitoring ” Button code , Yes StockMonitor Class instantiation in , And call StartMonitoring Function to monitor .

StockMonitorInst = None
def Button_6_onCommand(className,widgetName):
    global StockMonitorInst
    BtnText = Fun.GetUIText(className,widgetName)
    if BtnText == " Start the monitoring ":
        if StockMonitorInst == None:
            StockMonitorInst = StockMonitor.StockMonitor()
        StockArray = Fun.GetUIData(className,"TreeView_13","StockArray")
        for stockcode in StockArray:
            StockMonitorInst.addStock(stockcode[0],stockcode[1],stockcode[2])
        TreeView = Fun.GetUIEle(className,"TreeView_13")
        PhoneNumber=Fun.GetUIText(className,"Entry_17")
        StockMonitorInst.SetMonitorInfo(TreeView,PhoneNumber)       
        StockMonitorInst.StartMonitoring()    
        Fun.SetUIText(className,widgetName," Stop monitoring ")
    else:
        StockMonitorInst.StopMonitoring()
        Fun.SetUIText(className,widgetName," Start the monitoring ")

            If monitoring is not required , Click the button again .

copyright notice
author[Huoyun cave red boy],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201291352516869.html