-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstockmain160612.py
1153 lines (1016 loc) · 38.8 KB
/
stockmain160612.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# coding=utf-8
"""
@author: liujiawei (Sy1507518),hanxu (Sy1507522)
@license: GPL
@contact: [email protected],[email protected]
@see: http://blog.csdn.net/largetalk/article/details/6950435
@version: 3.1.1
@todo[0.0.2]: StockMarketSys
@note: a comment
@attention: please attention
@bug: a exist bug
@warning: warnings
"""
import sqlite3, random, threading, time, thread
import numpy as np
from Tkinter import *
from tkMessageBox import askokcancel
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg
from matplotlib.figure import Figure
class User:
"""
@Class describe: 用户类、属于事务管理层。包含所有当前用户操作,直接与表现层链接
@private param: __username 用户名
__balance 余额
__totalProperty 总资产
__profit 盈亏
__stockProperty 当前用户股票资产
__originalProperty 用户本金
"""
__username = ''
__balance = 10000
__totalProperty = 10000
__profit = 0
__stockProperty = 0 #
__originalProperty = 10000 # 用户本金
def __init__(self, name):
self.__username = name
def calBalance(self):
"""查询返回用户余额"""
db = Database('testDB.db', self.__username)
db.getLinkedUser(self.__username)
temp = db.getUserCurrentmoney()
return temp
def calToatalProperty(self):
"""查询返回用户总资产"""
db = Database('testDB.db', self.__username)
db.getLinkedUser(self.__username)
temp = db.getUserCurrentprop()
return temp
def calPrfLoss(self):
"""查询返回当前利润"""
return self.calToatalProperty() - self.__originalProperty
def calStockProp(self):
"""查询返回用户股票资产"""
db = Database('testDB.db', self.__username)
db.getLinkedUser(self.__username)
temp = db.getUserStockprop()
return temp
def update(self):
self.__balance = self.calBalance()
self.__totalProperty = self.calToatalProperty()
self.__profit = self.calPrfLoss()
self.__stockProperty = self.calStockProp()
return True
def buyOneStock(self, stockname, stocknum):
"""买入一支股票
:param stockname: 购买股票名称
:param stocknum: 购买数量
:return: BOOL
"""
db = Database('testDB.db', self.__username)
money = stocknum * db.getStockPrice(stockname)
flag1 = (bool)(db.getUserCurrentmoney() > money)
flag2 = (bool)(db.getStockBalance(stockname) > stocknum)
if flag1 and flag2:
db.changeUserStocknum(self.__username, stockname, stocknum)
db.changeStockBalance(stockname, -stocknum)
db.changeUserCurrentmoney(-money)
return True
else:
return False
def sellOneStock(self, stockname, stocknum):
"""卖出一支股票
:param stockname: 购买股票名称
:param stocknum: 购买数量
:return: BOOL
"""
db = Database('testDB.db', self.__username)
money = stocknum * db.getStockPrice(stockname)
if (bool)(db.getUserStocknum(self.__username, stockname) > stocknum):
db.changeUserStocknum(self.__username, stockname, stocknum)
db.changeStockBalance(stockname, -stocknum)
db.changeUserCurrentmoney(-money)
return True
else:
return False
def getUsername(self):
"""
:return: string用户名
"""
self.update()
return self.__username
def getBalance(self):
"""
:return: float用户现金余额
"""
self.update()
db = Database('testDB.db', self.__username)
self.__balance = db.getUserCurrentmoney()
return self.__balance
def getTotalProperty(self):
"""
:return: float用户现有总资产
"""
self.update()
return self.__totalProperty
def getProfitOrLoss(self):
"""
:return: float用户总资产盈亏
"""
self.update()
return round(self.__profit, 2)
def getStockProp(self):
"""
:return: float用户股票总额
"""
self.update()
return self.__stockProperty
def drawUserExchange(self, figure):
"""
用于在屏幕上显示用户持有的股票数量,显示方式为直方图图
:param figure: Figure
:return:
"""
db = Database('testDB.db', self.__username)
stocknamelist = db.getStocknameList()
userstocknumlist = db.getUserStocknnumlist(self.__username)
width = 0.5
a = figure.add_subplot(111)
ind = np.arange(1, len(stocknamelist) + 1, 1)
a.cla()
a.bar(ind - width / 2, userstocknumlist, width, color='green')
a.set_xticks(ind)
a.set_xticklabels(stocknamelist)
a.set_ylabel('Stock Num')
a.set_xlabel('Stock ID')
a.set_ylabel('Stock Num')
def getStockholdingMsg(self):
"""
:return: string 用户所有股票数量信息
"""
db = Database('testDB.db', self.__username)
return db.getUserStockholdingStr(self.__username)
class Stock():
"""
@Class describe: 股票类、属于事务管理层。包含所有股票市场的操作,直接与表现层链接
@private param: __username 用户名
"""
__username = ''
def __init__(self, username):
self.__username = username
return
def showStockprice(self, f):
"""
在屏幕上用柱状图的形式显示所有股票信息价格,包含了股票价格浮动功能,并且按照股价顺序列出各项股票
:param f: Figure
:return:
"""
"""
按照股价顺序排列各个股票
"""
db = Database('testDB.db', '')
self.floatStock()
stockpricelist = []
stocknamelist = []
dict2 = sorted(dict(zip(db.getStocknameList(), db.getStockpriceList())).iteritems(), key=lambda d: d[1],
reverse=True)
for i in range(len(dict2)):
stockpricelist.append(dict2[i][1])
stocknamelist.append(dict2[i][0])
a = f.add_subplot(111)
ind = np.arange(1, len(stocknamelist) + 1, 1)
width = 0.5
a.cla()
a.bar(ind - width / 2, stockpricelist, width, color='green')
a.set_xticks(ind)
a.set_xticklabels(stocknamelist)
a.set_xlabel(u'Stock ID')
a.set_ylabel(u'Stock Price')
return True
def floatStock(self):
"""
对股票市场进行价格浮动,10%以内上下随机浮动
:return: Bool
"""
db = Database('testDB.db', '')
stocknamelist = db.getStocknameList()
for n in stocknamelist:
temp = random.uniform(random.uniform(90, 110), random.uniform(90, 110)) / 100
price1 = ((float)(db.getStockPrice(n)))
price = round(price1 * temp, 2)
a = db.changeStockPrice(n, price)
return True
def addNewstock(self, stockname, stockprice, stocknum):
"""
在股票市场增加一支股票
:param stockname: 股票名称
:param stockprice: 股票价格
:param stocknum: 股票总数量
:return: Bool
"""
db = Database('testDB.db', '')
stocknamelist = db.getStocknameList()
if stockname in stocknamelist:
return False
else:
stocknamelist.append(stockname)
db.addnewstock(stockname, stockprice, stocknum)
return True
def initStock(self):
"""
用户股票数清零
:param : None
:return: None
"""
db = Database('testDB.db', self.__username)
db.initStock()
def deleteStock(self, stockName):
"""
在股票市场减少一支股票
:param stockname: 股票名称
:return: Bool
"""
db = Database('testDB.db', '')
stocknamelist = db.getStocknameList()
if stockName not in stocknamelist:
return False
else:
db.deleteStock(stockName)
return True
def getStockPrice(self, stockName):
"""
获取指定股票的价格
:param : stockName
:return:
"""
db = Database('testDB.db', '')
value = db.getStockPrice(stockName)
return value
def getStockNum(self, stockName):
"""
获取指定股票的市场存量
:param : stockName
:return:
"""
db = Database('testDB.db', '')
value = db.getStockCurrentnum(stockName)
return value
class Database():
"""
@Class describe: 数据库操作类、属于数据管理层。包含所有数据库相关操作,与事务处理层连接链接
@private param: __DBname 数据库名称,统一为testDB.db
__username 用户名
conn = sqlite3.connect('') 数据库链接参数
curs = conn.cursor() 数据库链接参数
sql = '' 数据库交互sql语句
"""
DBname = ''
__username = '' # 测试专用
conn = sqlite3.connect('')
curs = conn.cursor()
sql = ''
def __init__(self, DBname, username):
"""类的初始化操作,包括链接DataBase,赋值等
:param stockname: 数据库名称,股票名称
:return: float股票价格
"""
self.DBname = DBname
self.__username = username
self.conn = sqlite3.connect(self.DBname)
self.curs = self.conn.cursor()
return
def getLinkedStockprice(self):
"""
链接到stockprice
:return:
"""
self.conn = sqlite3.connect('testDB.db')
self.curs = self.conn.cursor()
def getStockPrice(self, stockname):
"""
链接stockprice表,获取某支股票价格
:param stockname: 股票名称
:return: float股票价格
"""
self.getLinkedStockprice()
stockprice = 0.0
sql = 'SELECT * FROM stockPri WHERE stock=? '
self.curs.execute(sql, (stockname,))
self.conn.commit()
value = self.curs.fetchall()
stockprice += value[0][1]
self.closeDatabase()
return round(stockprice, 2)
def getStockTotalnum(self, stockname):
"""
链接stockprice表,获取某支股票发行总数
:param stockname: 股票名称
:return: int股票数量
"""
self.getLinkedStockprice()
sql = 'SELECT * FROM stockPri WHERE stock=? '
self.curs.execute(sql, (stockname,))
self.conn.commit()
value = self.curs.fetchall()
stockTotalnum = value[0][3]
self.closeDatabase()
return stockTotalnum
def getStockCurrentnum(self, stockname):
"""
链接stockprice表,获取某支股票可购买数目
:param stockname: 股票名称
:return: int股票数量
"""
self.getLinkedStockprice()
sql = 'SELECT * FROM stockPri WHERE stock=? '
self.curs.execute(sql, (stockname,))
self.conn.commit()
value = self.curs.fetchall()
stockTotalnum = value[0][2]
self.closeDatabase()
return stockTotalnum
def getStockBalance(self, stockname):
"""
链接stockprice表,获取某支股票市场余量
:param stockname: 股票名称
:return: int股票剩余数量
"""
self.getLinkedStockprice()
sql = 'SELECT * FROM stockPri WHERE stock=? '
self.curs.execute(sql, (stockname,))
self.conn.commit()
value = self.curs.fetchall()
stockbalance = value[0][2]
self.closeDatabase()
return round((float)(stockbalance), 2)
def changeStockBalance(self, stockname, num):
"""
链接stockprice表,更改某支股票余额数量
:param stockname: 股票名称
:param num: 股票数量
:return: Bool
"""
temp = int(self.getStockBalance(stockname))
temp += num
if 0 < temp < self.getStockTotalnum(stockname):
self.getLinkedStockprice()
sql = 'update stockPri set num=? where stock=?'
self.curs.execute(sql, (temp, stockname))
self.conn.commit()
self.closeDatabase()
return True
else:
self.closeDatabase()
return False
def changeStockPrice(self, stockname, price):
"""
链接stockprice表,更改某支股票价格
:param stockname: 股票名称
:param price: 股票价格
:return: Bool
"""
self.getLinkedStockprice()
if 0 < price:
sql = 'UPDATE stockPri SET price=? WHERE stock=?'
self.curs.execute(sql, (price, stockname,))
self.conn.commit()
self.closeDatabase()
return True
else:
self.closeDatabase()
return False
def addnewstock(self, stockname, stockprice, stocknum):
"""
链接stockprice表,在其中增加一支股票
:param stockname: 股票名称
:param stockprice: 股票价格
:param stocknum: 股票数量
:return: Bool
"""
self.getLinkedStockprice()
sql = 'insert into stockPri (stock,price,num,totalnum) values(?,?,?,?)'
sql1 = 'insert into jiawei (stock,num) values(?,?)'
sql2 = 'insert into hanxu (stock,num) values(?,?)'
self.curs.execute(sql, (stockname, stockprice, stocknum, stocknum))
self.curs.execute(sql1, (stockname, 0))
self.curs.execute(sql2, (stockname, 0))
self.conn.commit()
self.closeDatabase()
return True
def initStock(self):
"""
链接userProp表,复位
:param stockname: None
:param stockprice: None
:param stocknum: None
:return: None
"""
stocknameList = self.getStocknameList()
self.getLinkedStockprice()
sql0 = 'update userProp set currentProp=? where username=?'
self.curs.execute(sql0, (10000, self.__username))
sql2 = 'update userProp set currentMoney=10000 where username=?'
self.curs.execute(sql2, (self.__username,))
for n in stocknameList:
try:
sql5 = 'select * from ' + self.__username + ' where stock=? '
self.curs.execute(sql5, (n,))
value = self.curs.fetchall()
userStockNum = value[0][1]
sql3 = 'select *from stockPri where stock=?'
self.curs.execute(sql3, (n,))
value = self.curs.fetchall()
temp = int(value[0][2]) + userStockNum
sql4 = 'update stockPri set num=? where stock=?'
self.curs.execute(sql4, (temp, n))
self.conn.commit()
sql1 = 'update ' + self.__username + ' set num=0 where stock=?'
self.curs.execute(sql1, (n,))
self.conn.commit()
except:
askokcancel('init fail')
self.closeDatabase()
def deleteStock(self, stockName):
"""
链接stockprice,user表,在其中减少一支股票
:param stockname: 股票名称
:return: Bool
"""
try:
self.getLinkedStockprice()
"""
此处应改为admin增加或者删除股票后,每个普通用户启动时,查询stockPri表,按照此表更新各自表的信息,增删股票代码,数量置为0
"""
sql0 = 'delete from stockPri where stock=?'
sql1 = 'delete from jiawei where stock=?'
sql2 = 'delete from hanxu where stock=?'
self.curs.execute(sql0, (stockName,))
self.curs.execute(sql1, (stockName,))
self.curs.execute(sql2, (stockName,))
self.conn.commit()
self.closeDatabase()
except:
return False
def getStocknameList(self):
"""
链接stockprice表,获取名字数组
:return: list数组
"""
self.getLinkedStockprice()
sql = 'select stock from stockPri'
self.curs.execute(sql)
self.conn.commit()
value = self.curs.fetchall()
stocknameList = []
for n in value:
stocknameList.append(n[0])
self.closeDatabase()
return stocknameList
def getStockpriceList(self):
"""
链接stockprice表,获取价格数组
:return: list数组
"""
self.getLinkedStockprice()
sql = 'select price from stockPri'
self.curs.execute(sql)
self.conn.commit()
value = self.curs.fetchall()
stockpricelist = []
for n in value:
stockpricelist.append(n[0])
self.closeDatabase()
return stockpricelist
"""
链接userx表相关函数
"""
def getLinkedUser(self, username):
"""
链接user表
:param username: 用户名
:return:
"""
self.__username = username
self.conn = sqlite3.connect('testDB.db')
self.curs = self.conn.cursor()
def getUserStocknum(self, username, stockname):
"""
链接user表,获取用户某支股票的数量
:param username: 用户名称
:param stockname: 股票名称
:return: int股票数量
"""
self.getLinkedUser(username)
sql = 'select * from ' + self.__username + ' where stock=?'
self.curs.execute(sql, (stockname,))
value = self.curs.fetchall()
userstocknum = value[0][1]
self.closeDatabase()
return userstocknum
def getUserStocknnumlist(self, username):
"""
链接user表,获取用户股票数量的数组
:param username: 股票名称
:return: list数组
"""
self.getLinkedUser(username)
sql = 'select num from ' + self.__username + ''
self.curs.execute(sql)
self.conn.commit()
value = self.curs.fetchall()
userStocknumlist = []
for n in value:
userStocknumlist.append(n[0])
self.closeDatabase()
return userStocknumlist
def getUserStockpricelist(self):
"""
链接user表,获取用户股票价格的数组
:param username: 股票名称
:return: list数组
"""
self.getLinkedStockprice()
sql = 'select price from stockPri'
self.curs.execute(sql)
self.conn.commit()
value = self.curs.fetchall()
userstockpricelist = []
for n in value:
userstockpricelist.append(n[0])
self.closeDatabase()
return userstockpricelist
def getUserStockprop(self):
"""
链接user表,获取用户股票总资产
:param username: 股票名称
:return: float股票资产
"""
userStockpricelist = self.getUserStockpricelist()
userStocknumlist = self.getUserStocknnumlist(self.__username)
money = sum(map(lambda(a,b):a*b, zip(userStockpricelist, userStocknumlist)))
return round(money, 2)
def changeUserStocknum(self, username, stockname, num):
"""
链接user表,更改用户股票数量
:param username: 用户名
:param stockname: 股票名称
:param num: 股票数量
:return: list数组
"""
self.getLinkedUser(username)
temp = int(num)
temp += self.getUserStocknum(username, stockname)
if -1 < temp < self.getStockTotalnum(stockname):
self.getLinkedUser(username)
sql = 'update ' + self.__username + ' set num=? where stock=?'
self.curs.execute(sql, (temp, stockname))
self.conn.commit()
self.closeDatabase()
return True
else:
askokcancel('交易失败', u'你股票余额不够,请重头来过!')
self.closeDatabase()
return False
def getUserStockholdingStr(self, username):
"""
链接user表,获取用户持有股票的信息
:param username: 用户名
:return: string持股信息
"""
userstocknumlist = self.getUserStocknnumlist(username)
userstocknamelist = self.getStocknameList()
i = 0
str1 = ''
for n in userstocknamelist:
str1 = str1 + 'StockName ' + n + ' StockNum : ' + str(userstocknumlist[i]) + '\n'
i += 1
return str1
def getLinkedUserprop(self):
"""
链接userPro表,用户资产表
:return:
"""
self.conn = sqlite3.connect('testDB.db')
self.curs = self.conn.cursor()
def updateUserCurrentprop(self):
"""
链接userPro表,更新数据库中用户现有总资产
:return:
"""
temp = self.getUserCurrentmoney() + self.getUserStockprop()
self.getLinkedUserprop()
sql = 'update userProp set currentProp=? where username=?'
self.curs.execute(sql, (temp, self.__username))
self.conn.commit()
self.closeDatabase()
def getUserOriginalprop(self):
"""
链接userPro表,获取用户原始资产
:return: float用户原始资产
"""
self.getLinkedUserprop()
sql = 'SELECT * FROM userProp WHERE username=? '
self.curs.execute(sql, (self.__username,))
self.conn.commit()
value = self.curs.fetchall()
originalprop = value[0][1]
self.closeDatabase()
return round(originalprop, 2)
def getUserCurrentprop(self):
"""
链接userPro表,获取用户当前资产
:return: float用户当前资产
"""
self.updateUserCurrentprop()
self.getLinkedUserprop()
sql = 'SELECT * FROM userProp WHERE username=? '
self.curs.execute(sql, (self.__username,))
self.conn.commit()
value = self.curs.fetchall()
currentprop = value[0][2]
self.closeDatabase()
return round(currentprop, 2)
def getUserCurrentmoney(self):
"""
链接userPro表,获取用户当前资金
:return: float用户当前资金
"""
self.getLinkedUserprop()
sql = 'SELECT * FROM userProp WHERE username=? '
self.curs.execute(sql, (self.__username,))
self.conn.commit()
value = self.curs.fetchall()
originalmoney = value[0][3]
self.closeDatabase()
return round((float)(originalmoney), 2)
def changeUserCurrentmoney(self, money):
"""
链接userPro表,改变用户当前资金
:param money: float,改变的金钱(正为增加、负为减少)
:return: Bool
"""
self.getLinkedUserprop()
temp = money + self.getUserCurrentmoney()
if 0 < temp:
self.getLinkedUserprop()
sql = 'update userProp set currentMoney=? where username=?'
self.curs.execute(sql, (temp, self.__username))
self.conn.commit()
self.closeDatabase()
return True
else:
self.closeDatabase()
return False
def getUseprofit(self):
"""
链接userPro表,获取用户盈利
:return: float,用户盈利
"""
return self.getUserCurrentprop() - self.getUserOriginalprop()
""""
链接namepassword表相关函数
"""
def getLinkedNamepassword(self, name, password):
"""
链接namepassword表,验证name和password是否匹配
:param name: 用户名
:param password: 密码
:return: Bool
"""
self.conn = sqlite3.connect('testDB.db')
self.curs = self.conn.cursor()
sql = 'SELECT * FROM namePassword where name=? '
try:
self.curs.execute(sql, (name,))
value = self.curs.fetchall()
a = value[0][1]
if a == password:
self.__username = name
self.closeDatabase()
return True
else:
self.closeDatabase()
return False
except:
self.closeDatabase()
return -1
def closeDatabase(self):
self.curs.close()
self.conn.commit()
self.conn.close()
class ViewFrame():
"""
@Class describe: 显示类、属于表现层。包含所有界面显示相关操作,与事务处理层链接
@param: __username 用户名
"""
__username = ''
def loginSys(self):
"""
登录界面
:return:
"""
root = Tk()
root.title("欢迎进入股票交易系统!")
root.geometry('500x300')
def quit():
"""
销毁登录界面
:return:
"""
root.quit()
root.destroy()
def enter():
"""
登陆按钮响应函数,成功则进入股票显示界面
:return:
"""
myName = personName.get()
myPassword = password.get()
myName = myName.lower()
db = Database('testDB.db', self.__username)
temp = db.getLinkedNamepassword(myName, myPassword)
if 1 == temp:
self.__username = myName
quit()
self.showStock()
elif 0 == temp:
contents.delete(1.0, END)
contents.insert(END, u"密码错误,请输入正确密码!(●'◡'●)")
elif -1 == temp:
contents.delete(1.0, END)
contents.insert(END, u"用户不存在,请核实用户名重新输入( ▼-▼ )")
L1 = Label(root, text="用户名", font=("微软雅黑", 12), width=6, height=2)
L1.grid(row=0, column=0, sticky=E)
personName = Entry(root)
personName.grid(row=0, column=1, sticky=E)
L2 = Label(root, text="密码 ", font=("微软雅黑", 12), width=6, height=2)
L2.grid(row=1, column=0, sticky=E)
password = Entry(root)
password.grid(row=1, column=1, sticky=E)
root.bind('<Return>', enter)
ButtonEnter = Button(root, text="登陆", command=enter, bg="grey", font=("微软雅黑", 12), width=5, height=2)
ButtonEnter.grid(row=0, column=2, columnspan=1, rowspan=2, sticky=E, padx=30, pady=30)
contents = Text(root)
contents.grid(row=2, column=0, columnspan=4, rowspan=1)
contents.delete(1.0, END)
contents.insert(END, '请在上方输入用户名称和密码进入股票交易系统q(≧▽≦q)')
mainloop()
def showStock(self):
"""
股价显示界面
:return:
"""
root = Tk()
root.wm_title('股票市场 当前用户 : ' + self.__username)
root.geometry('720x480')
f = Figure(figsize=(5, 5), dpi=90)
def relogin():
"""
返回登陆界面
:return:
"""
quit()
self.loginSys()
def quit():
"""
销毁当前窗口
:return:
"""
root.quit()
root.destroy()
def draw_picture():
"""
绘制股票显示图表
:return:
"""
stk = Stock('')
stk.showStockprice(f)
canvas.draw()
def addNewstock():
"""
增加一支新股票,addnewstock的响应函数
:return:
"""
stockname = stock.get()
stocknum = int(num.get())
stockpri = float(stockprice.get())
stk = Stock('')
stk.addNewstock(stockname, stockpri, stocknum)
draw_picture()
def trade():
"""
销毁股票显示界面,进入trade按钮的响应函数
:return:
##"""
quit()
self.exchange()
def deleteStock():
"""
删除一支新股票,deleteStock的响应函数
:return:
"""
stockname = stock.get()
stk = Stock('')
stk.deleteStock(stockname)
draw_picture()
def findDB():
"""
查找一支新股票,获取其市场余量和价格
:return:
"""
try:
stockName = stock.get()
stk = Stock('')
num.delete(0, END)
str0 = stk.getStockNum(stockName)
num.insert(0, str0)
stockprice.delete(0, END)
str0 = stk.getStockPrice(stockName)
stockprice.insert(0, str0)
except:
askokcancel('查询失败', u'请检查股票代码!')
canvas = FigureCanvasTkAgg(f, root)
canvas.get_tk_widget().grid(row=0, column=0, columnspan=4, rowspan=6, sticky=W + N)
if self.__username == 'admin':
L1 = Label(root, text="股票代码", font=("微软雅黑", 12), width=10, height=1)
L1.grid(row=0, column=5, sticky=E)
stock = Entry(root)
stock.grid(row=0, column=6, sticky=E)
L2 = Label(root, text="股票数目", font=("微软雅黑", 12), width=10, height=1)
L2.grid(row=1, column=5, sticky=W)
num = Entry(root)
num.grid(row=1, column=6, sticky=E)
L3 = Label(root, text="股票价格", font=("微软雅黑", 12), width=10, height=1)
L3.grid(row=2, column=5, sticky=W)
stockprice = Entry(root)
stockprice.grid(row=2, column=6, sticky=E)
Button(root, text="新股发行", command=addNewstock, bg="grey", width=10, height=1).grid(row=3, column=5,
sticky=E)
Button(root, text="股票退市", command=deleteStock, bg="grey", width=10, height=1).grid(row=3, column=6,
sticky=E)
Button(root, text="股市刷新", command=draw_picture, bg="grey", width=10, height=1).grid(row=4, column=5,
sticky=E)
Button(root, text="退出系统", command=quit, bg="grey", width=10, height=1).grid(row=4, column=6, sticky=E)
Button(root, text="重新登录", command=relogin, bg="grey", width=10, height=1).grid(row=5, column=5, sticky=E)
Button(root, text="股票查询", command=findDB, bg="grey", width=10, height=1).grid(row=5, column=6, sticky=E)
if self.__username != 'admin':
L1 = Label(root, text="股票代码", font=("微软雅黑", 12), width=10, height=1)
L1.grid(row=0, column=5, sticky=E)
stock = Entry(root)
stock.grid(row=0, column=6, sticky=E)
L2 = Label(root, text="股票余量", font=("微软雅黑", 12), width=10, height=1)
L2.grid(row=1, column=5, sticky=W)
num = Entry(root)
num.grid(row=1, column=6, sticky=E)
L3 = Label(root, text="股票价格", font=("微软雅黑", 12), width=10, height=1)
L3.grid(row=2, column=5, sticky=W)
stockprice = Entry(root)
stockprice.grid(row=2, column=6, sticky=E)
Button(root, text="用户交易", command=trade, bg="grey", width=10, height=1).grid(row=3, column=5, sticky=E)
Button(root, text="股市刷新", command=draw_picture, bg="grey", width=10, height=1).grid(row=3, column=6,
sticky=E)
Button(root, text="退出系统", command=quit, bg="grey", width=10, height=1).grid(row=4, column=5, sticky=E)
Button(root, text="重新登录", command=relogin, bg="grey", width=10, height=1).grid(row=4, column=6, sticky=E)
Button(root, text="股票查询", command=findDB, bg="grey", width=10, height=1).grid(row=5, column=5, sticky=E)
draw_picture()
askokcancel('欢迎!', '尊敬的' + self.__username + '欢迎进入股票交易系统!')
mainloop()
def exchange(self):
"""
用户信息及交易显示界面
:return:
"""
root = Tk()
root.resizable(width=True, height=True)
root.wm_title("用户交易 当前用户 : " + self.__username)
f = Figure(figsize=(7, 5), dpi=80)
def check():
"""刷新股仓,刷新图像显示,显示用户的资产变化情况。