Skip to content

Commit e57a879

Browse files
Add Stock Monitor Examples
Add Stock Monitor Examples
1 parent 31c1028 commit e57a879

File tree

5 files changed

+390
-0
lines changed

5 files changed

+390
-0
lines changed

Examples/StockPrice/Fun.py

Lines changed: 177 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,177 @@
1+
#This file does not support direct editing
2+
#coding=utf-8
3+
import os
4+
from os.path import abspath, dirname
5+
import tkinter
6+
import tkinter.simpledialog
7+
8+
G_ElementBindingDataArray={}
9+
G_UIElementArray={}
10+
G_UIElementVariableArray={}
11+
G_UIInputDataArray={}
12+
#Add Element to G_UIElementArray:Param1:uiName, Param2:elementName,Param3:element
13+
def AddElement(uiName,elementName,element):
14+
if uiName not in G_UIElementArray:
15+
G_UIElementArray[uiName]={}
16+
G_UIElementArray[uiName][elementName]=element
17+
#Add Data to G_UIElementVariableArray:Param1:uiName, Param2:elementName
18+
def AddElementVariable(uiName,elementName):
19+
if uiName not in G_UIElementVariableArray:
20+
G_UIElementVariableArray[uiName]={}
21+
NameLower = elementName.lower()
22+
if NameLower.find('combobox_') >= 0:
23+
G_UIElementVariableArray[uiName][elementName]=tkinter.IntVar()
24+
elif NameLower.find('group_') >= 0:
25+
G_UIElementVariableArray[uiName][elementName]=tkinter.IntVar()
26+
elif NameLower.find('checkbutton_') >= 0:
27+
G_UIElementVariableArray[uiName][elementName]=tkinter.BooleanVar()
28+
else:
29+
G_UIElementVariableArray[uiName][elementName]=tkinter.StringVar()
30+
return G_UIElementVariableArray[uiName][elementName]
31+
#Set Data of G_UIElementVariableArray:Param1:uiName, Param2:elementName, Param3:value
32+
def SetElementVariable(uiName,elementName,value):
33+
if uiName in G_UIElementVariableArray:
34+
if elementName in G_UIElementVariableArray[uiName]:
35+
G_UIElementVariableArray[uiName][elementName].set(value)
36+
#Get Data of G_UIElementVariableArray:Param1:uiName, Param2:elementName
37+
def GetElementVariable(uiName,elementName):
38+
if uiName in G_UIElementVariableArray:
39+
if elementName in G_UIElementVariableArray[uiName]:
40+
return G_UIElementVariableArray[uiName][elementName].get()
41+
#Add Element 's BindingData :Param1:uiName, Param2:elementName,Param3:DataName,Param4:datatype, Param5: DataValue, Param6: isMapToText
42+
def AddUIData(uiName,elementName,dataName,datatype,datavalue,isMapToText):
43+
global G_ElementBindingDataArray
44+
if uiName not in G_ElementBindingDataArray:
45+
G_ElementBindingDataArray[uiName]={}
46+
if elementName not in G_ElementBindingDataArray[uiName]:
47+
G_ElementBindingDataArray[uiName][elementName]=[]
48+
G_ElementBindingDataArray[uiName][elementName].append([dataName,datatype,datavalue,isMapToText])
49+
#Set Element 's BindingData :Param1:uiName, Param2:elementName,Param3:DataName,Param4:DataValue
50+
def SetUIData(uiName,elementName,dataName,datavalue):
51+
global G_UIElementArray
52+
global G_ElementBindingDataArray
53+
if uiName in G_ElementBindingDataArray:
54+
if elementName in G_ElementBindingDataArray[uiName]:
55+
for EBData in G_ElementBindingDataArray[uiName][elementName]:
56+
if EBData[0] == dataName:
57+
EBData[2] = datavalue
58+
if EBData[3] == 1:
59+
SetUIText(uiName,elementName,datavalue)
60+
return
61+
#Get Element 's BindingData :Param1:uiName, Param2:elementName,Param3:DataName
62+
def GetUIData(uiName,elementName,dataName):
63+
global G_ElementBindingDataArray
64+
if uiName in G_ElementBindingDataArray:
65+
if elementName in G_ElementBindingDataArray[uiName]:
66+
for EBData in G_ElementBindingDataArray[uiName][elementName]:
67+
if EBData[0] == dataName:
68+
if EBData[1]=='int':
69+
return int(EBData[2])
70+
elif EBData[1]=='float':
71+
return float(EBData[2])
72+
else:
73+
return EBData[2]
74+
return None
75+
#Set Element 's Attrib :Param1:uiName, Param2:elementName,Param3:AttribName,Param4:AttribValue
76+
def SetUIAttrib(uiName,elementName,AttribName,attribValue):
77+
global G_UIElementArray
78+
if uiName in G_UIElementArray:
79+
G_UIElementArray[uiName][elementName].configure(AttribName=attribValue)
80+
#Get Element's Attrib :Param1:uiName, Param2:elementName,Param3:attributeName
81+
def GetUIAttrib(uiName,elementName,AttribName):
82+
global G_UIElementArray
83+
if uiName in G_UIElementArray:
84+
return G_UIElementArray[uiName][elementName].cget(AttribName)
85+
return None
86+
#Get Element:Param1:uiName, Param2:elementName
87+
def GetUIEle(uiName,elementName):
88+
global G_UIElementArray
89+
if uiName in G_UIElementArray:
90+
return G_UIElementArray[uiName][elementName]
91+
#Set Element 's Text:Param1:uiName, Param2:elementName,Param3:textValue
92+
def SetUIText(uiName,elementName,textValue):
93+
global G_UIElementArray
94+
global G_UIElementVariableArray
95+
showtext = str("%s"%textValue)
96+
if uiName in G_UIElementVariableArray:
97+
if elementName in G_UIElementVariableArray[uiName]:
98+
G_UIElementVariableArray[uiName][elementName].set(showtext)
99+
return
100+
if uiName in G_UIElementArray:
101+
if elementName in G_UIElementArray[uiName]:
102+
if elementName.find('Text_') >= 0:
103+
G_UIElementArray[uiName][elementName].delete('0.0',tkinter.END)
104+
G_UIElementArray[uiName][elementName].insert(tkinter.END,showtext)
105+
else:
106+
G_UIElementArray[uiName][elementName].configure(text=showtext)
107+
#Get Element 's Text:Param1:uiName, Param2:elementName
108+
def GetUIText(uiName,elementName):
109+
global G_UIElementArray
110+
global G_UIElementVariableArray
111+
if uiName in G_UIElementVariableArray:
112+
if elementName in G_UIElementVariableArray[uiName]:
113+
return G_UIElementVariableArray[uiName][elementName].get()
114+
if uiName in G_UIElementArray:
115+
if elementName in G_UIElementArray[uiName]:
116+
if elementName.find('Text_') >= 0:
117+
return G_UIElementArray[uiName][elementName].get('0.0', tkinter.END)
118+
else:
119+
return G_UIElementArray[uiName][elementName].cget('text')
120+
return str("")
121+
#Init Element 's Data:Param1:uiName
122+
def InitElementData(uiName):
123+
global G_ElementBindingDataArray
124+
if uiName in G_ElementBindingDataArray:
125+
for elementName in G_ElementBindingDataArray[uiName].keys():
126+
for EBData in G_ElementBindingDataArray[uiName][elementName]:
127+
if EBData[3] == 1:
128+
SetUIText(uiName,elementName,EBData[2])
129+
SetUIText(uiName,elementName,EBData[2])
130+
#Update Element 's Input Data Array:Param1:uiName
131+
def UpdateUIInputDataArray(uiName):
132+
global G_UIElementArray
133+
global G_UIInputDataArray
134+
global G_UIElementVariableArray
135+
G_UIInputDataArray.clear()
136+
if uiName in G_UIElementArray:
137+
for elementName in G_UIElementArray[uiName].keys():
138+
G_UIInputDataArray[elementName] = []
139+
Widget = G_UIElementArray[uiName][elementName]
140+
if elementName.find('Text_') >= 0:
141+
content = Widget.get('0.0', tkinter.END)
142+
G_UIInputDataArray[elementName].append(content)
143+
elif elementName.find('Entry_') >= 0:
144+
content = G_UIElementVariableArray[uiName][elementName].get()
145+
G_UIInputDataArray[elementName].append(content)
146+
if uiName in G_UIElementVariableArray:
147+
for elementName in G_UIElementVariableArray[uiName].keys():
148+
if elementName.find('Group_') >= 0:
149+
ElementIntValue = G_UIElementVariableArray[uiName][elementName].get()
150+
G_UIInputDataArray[elementName] = []
151+
G_UIInputDataArray[elementName].append(ElementIntValue)
152+
return G_UIInputDataArray
153+
#MessageBox
154+
def MessageBox(text):
155+
tkinter.messagebox.showwarning('info',text)
156+
#InputBox
157+
def InputBox(title,text):
158+
res = tkinter.simpledialog.askstring(title,'Input Box',initialvalue=text)
159+
return res
160+
#Return a file list from dir
161+
def WalkAllResFiles(parentPath,alldirs=True):
162+
ResultFilesArray = []
163+
if os.path.exists(parentPath) == True:
164+
for fileName in os.listdir(parentPath):
165+
if '__pycache__' not in fileName:
166+
if '.git' not in fileName:
167+
newPath = parentPath +'\\'+ fileName
168+
if os.path.isdir(newPath):
169+
ResultFilesArray.append(newPath)
170+
if alldirs == True:
171+
ResultFilesArray.extend(WalkAllResFiles(newPath,alldirs))
172+
else:
173+
ResultFilesArray.append(newPath)
174+
return ResultFilesArray
175+
#Add params to event functions
176+
def EventFunction_Adaptor(fun, **params):
177+
return lambda event, fun=fun, params=params: fun(event, **params)
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import os
2+
import time
3+
import tushare as ts
4+
import pandas as pd
5+
import threading
6+
from twilio.rest import Client
7+
threadactive = True
8+
def getStockInfo(stock_code):
9+
df = ts.get_realtime_quotes(stock_code)
10+
# e = df[['code','name','time','price']]
11+
c = df[u'code']
12+
n = df[u'name']
13+
t = df[u'time']
14+
p = df[u'price']
15+
text = c[0] + " " + n[0] + " " + t[0] + " " + p[0]
16+
print(text)
17+
return df
18+
class StockMonitor:
19+
def __init__(self):
20+
self.StockArray = []
21+
self.TreeView = None
22+
self.PhoneNumber = None
23+
self.Run_Function = None
24+
def addStock(self,StockCode,MaxPrice,MinPrice):
25+
self.StockArray.append([StockCode,MaxPrice,MinPrice,False,False])
26+
def SetMonitorInfo(self,TreeView,PhoneNumber):
27+
self.TreeView = TreeView
28+
self.PhoneNumber = PhoneNumber
29+
def StartMonitoring(self):
30+
account_sid = ""#改成申请的SID
31+
auth_token = ""#改成申请的TOKEN
32+
client = Client(account_sid, auth_token)
33+
all_items = self.TreeView.get_children()
34+
for item in all_items:
35+
self.TreeView.delete(item)
36+
for stock_code in self.StockArray:
37+
df = ts.get_realtime_quotes(stock_code[0])
38+
e = df[['code','name','price','time']]
39+
c = df[u'code']
40+
n = df[u'name']
41+
p = df[u'price']
42+
t = df[u'time']
43+
print(e)
44+
if float(p[0]) > float(stock_code[2]) :
45+
self.TreeView.insert('','end',values=(c[0],n[0],t[0],p[0],stock_code[2],stock_code[1],"超过上限"))
46+
if stock_code[3] == False:
47+
# smsText = "您的股票"+n[0]+"("+c[0]+") 已超过设定上限价格"+ stock_code[2] + ",当前价格"+p[0]+",请及时关注!"
48+
smsText = "Stock:"+"("+c[0]+") Price:"+p[0]
49+
#from_改成得到的试用手机号
50+
client.messages.create(to=self.PhoneNumber,from_="+13343784863",body=smsText)
51+
stock_code[3] = True
52+
elif float(p[0]) < float(stock_code[1]):
53+
self.TreeView.insert('','end',values=(c[0],n[0],t[0],p[0],stock_code[2],stock_code[1],"低于下限"))
54+
if stock_code[4] == False:
55+
# smsText = "您的股票"+n[0]+"("+c[0]+") 已低于设定下限价格"+ stock_code[1] + ",当前价格"+p[0]+",请及时关注!"
56+
smsText = "Stock:"+"("+c[0]+") Price:"+p[0]
57+
print(smsText)
58+
#from_改成得到的试用手机号
59+
client.messages.create(to=self.PhoneNumber,from_="+13343784863",body=smsText)
60+
stock_code[4] = True
61+
else:
62+
self.TreeView.insert('','end',values=(c[0],n[0],t[0],p[0],stock_code[1],stock_code[2],"平稳波动"))
63+
self.Run_Function = self.TreeView.after(1000, self.StartMonitoring)
64+
def StopMonitoring(self):
65+
self.TreeView.after_cancel(self.Run_Function)
66+
self.StockArray = []

Examples/StockPrice/StockPrice.py

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
#coding=utf-8
2+
3+
#import libs
4+
import StockPrice_cmd
5+
import Fun
6+
import tkinter
7+
from tkinter import *
8+
import tkinter.ttk
9+
import tkinter.font
10+
ElementBGArray={}
11+
ElementBGArray_Resize={}
12+
ElementBGArray_IM={}
13+
14+
#Add your Varial Here: (Keep This Line of comments)
15+
#Define UI Class
16+
class StockPrice:
17+
def __init__(self,root,isTKroot = True):
18+
className = self.__class__.__name__
19+
Fun.G_UIElementArray[className]={}
20+
Fun.G_ElementBindingDataArray[className]={}
21+
global ElementBGArray
22+
global ElementBGArray_Resize
23+
global ElementBGArray_IM
24+
Fun.AddElement(className,'UIClass',self)
25+
self.root = root
26+
if isTKroot == True:
27+
root.title("股票监控")
28+
root.geometry("906x363")
29+
Form_1= tkinter.Canvas(root,width = 10,height = 4)
30+
Form_1.place(x = 0,y = 0,width = 906,height = 363)
31+
Form_1.configure(bg = "#efefef")
32+
Fun.AddElement(className,'root',root)
33+
Fun.AddElement(className,'Form_1',Form_1)
34+
#Create the elements of root
35+
Label_2= tkinter.Label(root,text="股票代码",width = 10,height = 4)
36+
Label_2.place(x = -22,y = 27,width = 107,height = 22)
37+
Fun.AddElement(className,'Label_2',Label_2)
38+
Entry_3_Variable = Fun.AddElementVariable(className,'Entry_3')
39+
Entry_3= tkinter.Entry(root,textvariable=Entry_3_Variable)
40+
Entry_3.place(x = 68,y = 29,width = 164,height = 20)
41+
Entry_3.configure(relief = "sunken")
42+
Fun.AddElement(className,'Entry_3',Entry_3)
43+
Button_4= tkinter.Button(root,text="增加股票",width = 10,height = 4)
44+
Button_4.place(x = 463,y = 24,width = 100,height = 28)
45+
Button_4.configure(command=lambda:StockPrice_cmd.Button_4_onCommand(className,"Button_4"))
46+
Fun.AddElement(className,'Button_4',Button_4)
47+
Button_6= tkinter.Button(root,text="启动监控",width = 10,height = 4)
48+
Button_6.place(x = 790,y = 25,width = 100,height = 28)
49+
Button_6.configure(command=lambda:StockPrice_cmd.Button_6_onCommand(className,"Button_6"))
50+
Fun.AddElement(className,'Button_6',Button_6)
51+
Label_9= tkinter.Label(root,text="最高价",width = 10,height = 4)
52+
Label_9.place(x = 179,y = 27,width = 55,height = 20)
53+
Fun.AddElement(className,'Label_9',Label_9)
54+
Entry_10_Variable = Fun.AddElementVariable(className,'Entry_10')
55+
Entry_10= tkinter.Entry(root,textvariable=Entry_10_Variable)
56+
Entry_10.place(x = 380,y = 28,width = 76,height = 20)
57+
Entry_10.configure(relief = "sunken")
58+
Fun.AddElement(className,'Entry_10',Entry_10)
59+
Label_11= tkinter.Label(root,text="最低价",width = 10,height = 4)
60+
Label_11.place(x = 315,y = 25,width = 55,height = 20)
61+
Fun.AddElement(className,'Label_11',Label_11)
62+
Entry_12_Variable = Fun.AddElementVariable(className,'Entry_12')
63+
Entry_12= tkinter.Entry(root,textvariable=Entry_12_Variable)
64+
Entry_12.place(x = 235,y = 26,width = 70,height = 20)
65+
Entry_12.configure(relief = "sunken")
66+
Fun.AddElement(className,'Entry_12',Entry_12)
67+
TreeView_13= tkinter.ttk.Treeview(root,show="tree")
68+
TreeView_13.place(x = 17,y = 65,width = 875,height = 291)
69+
TreeView_13.configure(show = "headings")
70+
TreeView_13.configure(selectmode = "extended")
71+
TreeView_13.configure(columns = ["股票代码","股票名称","当前时间","当前价","限制最高","限制最低","警报"])
72+
TreeView_13.column("股票代码",anchor="center",width=90)
73+
TreeView_13.heading("股票代码",text="股票代码")
74+
TreeView_13.column("股票名称",anchor="center",width=90)
75+
TreeView_13.heading("股票名称",text="股票名称")
76+
TreeView_13.column("当前时间",anchor="center",width=90)
77+
TreeView_13.heading("当前时间",text="当前时间")
78+
TreeView_13.column("当前价",anchor="center",width=80)
79+
TreeView_13.heading("当前价",text="当前价")
80+
TreeView_13.column("限制最高",anchor="center",width=80)
81+
TreeView_13.heading("限制最高",text="限制最高")
82+
TreeView_13.column("限制最低",anchor="center",width=80)
83+
TreeView_13.heading("限制最低",text="限制最低")
84+
TreeView_13.column("警报",anchor="center",width=100)
85+
TreeView_13.heading("警报",text="警报")
86+
Fun.AddUIData(className,'TreeView_13','StockArray','list',[],0)
87+
Fun.AddElement(className,'TreeView_13',TreeView_13)
88+
Label_16= tkinter.Label(root,text="通知手机",width = 10,height = 4)
89+
Label_16.place(x = 571,y = 26,width = 81,height = 23)
90+
Fun.AddElement(className,'Label_16',Label_16)
91+
Entry_17_Variable = Fun.AddElementVariable(className,'Entry_17')
92+
Entry_17= tkinter.Entry(root,textvariable=Entry_17_Variable)
93+
Entry_17.place(x = 652,y = 28,width = 126,height = 20)
94+
Entry_17.configure(relief = "sunken")
95+
Fun.AddElement(className,'Entry_17',Entry_17)
96+
#Inital all element's Data
97+
Fun.InitElementData(className)
98+
#Add Some Logic Code Here: (Keep This Line of comments)
99+
100+
#Create the root of Kinter
101+
if __name__ == '__main__':
102+
root = tkinter.Tk()
103+
MyDlg = StockPrice(root)
104+
root.mainloop()
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#coding=utf-8
2+
import sys
3+
import os
4+
from os.path import abspath, dirname
5+
sys.path.append(abspath(dirname(__file__)))
6+
import tkinter
7+
import tkinter.filedialog
8+
from tkinter import *
9+
import Fun
10+
import StockMonitor
11+
def Button_4_onCommand(className,widgetName):
12+
StockCode = Fun.GetUIText(className,"Entry_3")
13+
StockInfo = StockMonitor.getStockInfo(StockCode)
14+
StockCode = StockInfo[u'code']
15+
StockName = StockInfo[u'name']
16+
StockTime = StockInfo[u'time']
17+
StockPrice = StockInfo[u'price']
18+
MaxPrice = Fun.GetUIText(className,"Entry_10")
19+
MinPrice =Fun.GetUIText(className,"Entry_12")
20+
TreeView13 = Fun.GetUIEle(className,"TreeView_13")
21+
TreeView13 .insert('','end',values=(StockCode[0],StockName[0],StockTime[0],StockPrice[0],MinPrice,MaxPrice))
22+
StockArray = Fun.GetUIData(className,"TreeView_13","StockArray")
23+
StockArray.append([StockCode[0],MaxPrice,MinPrice])
24+
StockMonitorInst = None
25+
def Button_6_onCommand(className,widgetName):
26+
global StockMonitorInst
27+
BtnText = Fun.GetUIText(className,widgetName)
28+
29+
if BtnText == "启动监控":
30+
if StockMonitorInst == None:
31+
StockMonitorInst = StockMonitor.StockMonitor()
32+
StockArray = Fun.GetUIData(className,"TreeView_13","StockArray")
33+
for stockcode in StockArray:
34+
StockMonitorInst.addStock(stockcode[0],stockcode[1],stockcode[2])
35+
TreeView = Fun.GetUIEle(className,"TreeView_13")
36+
PhoneNumber=Fun.GetUIText(className,"Entry_17")
37+
StockMonitorInst.SetMonitorInfo(TreeView,PhoneNumber)
38+
StockMonitorInst.StartMonitoring()
39+
Fun.SetUIText(className,widgetName,"停止监控")
40+
else:
41+
StockMonitorInst.StopMonitoring()
42+
Fun.SetUIText(className,widgetName,"启动监控")
43+

TKinterDesigner.exe

-4.13 KB
Binary file not shown.

0 commit comments

Comments
 (0)