爱彩乐十一选五

首頁 > 上網技巧 > 電腦小技巧 > wxpython簡單記錄生詞GUI程序

wxpython簡單記錄生詞GUI程序

時間:2019-10-04 16:26 作者:QQ地帶 我要評論

不使用數據庫,實現一個簡單的記生詞軟件,基本功能包括,添加新的生詞及其中文含義,瀏覽已經記錄的單詞,隨機選擇部分單詞進行復習。可考慮其它拓展的功能。
 
這里使用wxpython的基本操作,以及類與函數的調用方式。
 
Python
 
import random
import wx
mydic = {}
num = []
taged = []
class InfoPanel(wx.Frame):
def __init__(self, parent, id):
wx.Frame.__init__(self, parent, id, "詞典",  pos=(0, 0), size=(450, 400))
panel = wx.Panel(self, -1)
rev = wx.StaticText(panel, -1, '英文:', pos=(3, 3))
rev = wx.StaticText(panel, -1, '中文:', pos=(170, 3))
rev = wx.StaticText(panel, -1, 'English:', pos=(50, 50))
rev = wx.StaticText(panel, -1, 'Chinese:', pos=(250, 50))
rev = wx.StaticText(panel, -1, '記錄單詞總數:', pos=(50, 29))
rev.SetForegroundColour("black")
rev.SetBackgroundColour("")   # 文字背景顏色,不輸入為透明
self.temp_Eng = wx.TextCtrl(panel, -1, "", pos=(40, 0), size=(100, -1))
self.temp_ZH = wx.TextCtrl(panel, -1, "", pos=(207, 0), size=(100, -1))
button = wx.Button(panel, wx.ID_ANY, pos=(320, 0), size=(80, 26), label='增加單詞')
button.Bind(wx.EVT_BUTTON, self.get_word)
button2 = wx.Button(panel, wx.ID_ANY, pos=(0, 200), size=(430, 30), label='查看所有單詞')
button2.Bind(wx.EVT_BUTTON, self.scan_word)
button3 = wx.Button(panel, wx.ID_ANY, pos=(0, 150), size=(140, 30), label='隨機學習')
button3.Bind(wx.EVT_BUTTON, self.review_word)
button4 = wx.Button(panel, wx.ID_ANY, pos=(145, 150), size=(140, 30), label='標記')
button4.Bind(wx.EVT_BUTTON, self.tag_button)
button5 = wx.Button(panel, wx.ID_ANY, pos=(290, 150), size=(140, 30), label='隨機查看標記單詞')
button5.Bind(wx.EVT_BUTTON, self.get_taged_word)
self.text = wx.TextCtrl(panel, -1, "", pos=(3, 70), size=(170, 50))
self.text2 = wx.TextCtrl(panel, -1, "", pos=(200, 70), size=(170, 50))
self.text3 = wx.TextCtrl(panel, -1, "", pos=(0, 230), size=(430, 110), style=wx.TE_MULTILINE)
self.total_word = wx.TextCtrl(panel, -1, "", pos=(140, 27), size=(40, 25))
self.ran = None
self.load_file(self)
self.get_total_word(self)
def get_total_word(self, event):
global num
self.total_word.AppendText(str(len(num)))
def write_word2file(self, Eng, ZH):
f = open(".\dic.txt", 'a+')
f.write(Eng + ':'+ZH + '\n')
f.close()
def get_word(self, event):
global num
if self.temp_Eng.GetValue() != None or self.temp_ZH.GetValue() != None:
self.write_word2file(self.temp_Eng.GetValue(), self.temp_ZH.GetValue())
self.temp_Eng.Clear()
self.temp_ZH.Clear()
self.total_word.Clear()
self.load_file(self)
self.total_word.AppendText(str(len(num)))
def load_file(self, event):
global mydic
global num
f = open("E:\\dic.txt", 'r')
for line in f.readlines():
if ':' in line and line[:len(line)-1] not in num:
mydic[line[:line.index(':')]] = line[line.index(':')+1:len(line)-1]
num.append(line[:len(line)-1])
def scan_word(self, event):
global num
self.load_file(event)
self.text.Clear()
for _ in range(len(num)):
self.text3.AppendText(num[_]+'\n')
def review_word(self, event):
global num
self.text.Clear()
self.text2.Clear()
self.ran = random.randint(0, len(num)-1)
while self.ran == random.randint(0, len(num)-1):
self.ran = random.randint(0, len(num) - 1)
self.text.AppendText(num[self.ran][:num[self.ran].index(':')])
self.text2.AppendText(num[self.ran][num[self.ran].index(':')+1:])
def tag_button(self, event):
if self.ran != None:
self.tag_word(self.ran)
def tag_word(self, event):
global num
global taged
if num[self.ran] not in taged:
taged.append(num[self.ran])
print(num)
def get_taged_word(self, event):
global taged
self.text.Clear()
self.text2.Clear()
_ = random.randint(0, len(taged)-1)
while _ == random.randint(0, len(taged)-1):
_ = random.randint(0, len(taged) - 1)
self.text.AppendText(taged[_][:taged[_].index(':')])
self.text2.AppendText(taged[_][taged[_].index(':') + 1:])
class MainApp(wx.App):
def OnInit(self):
self.frame1 = InfoPanel(None, -1)
self.frame1.Center()
self.frame1.Show(True)
self.SetTopWindow(self.frame1)
return True
if __name__ == '__main__':
app = MainApp(0)
app.MainLoop()
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
import random
import wx
 
mydic = {}
num = []
taged = []
 
 
class InfoPanel(wx.Frame):
    def __init__(self, parent, id):
        wx.Frame.__init__(self, parent, id, "詞典",  pos=(0, 0), size=(450, 400))
        panel = wx.Panel(self, -1)
        rev = wx.StaticText(panel, -1, '英文:', pos=(3, 3))
        rev = wx.StaticText(panel, -1, '中文:', pos=(170, 3))
        rev = wx.StaticText(panel, -1, 'English:', pos=(50, 50))
        rev = wx.StaticText(panel, -1, 'Chinese:', pos=(250, 50))
        rev = wx.StaticText(panel, -1, '記錄單詞總數:', pos=(50, 29))
        rev.SetForegroundColour("black")
        rev.SetBackgroundColour("")   # 文字背景顏色,不輸入為透明
        self.temp_Eng = wx.TextCtrl(panel, -1, "", pos=(40, 0), size=(100, -1))
        self.temp_ZH = wx.TextCtrl(panel, -1, "", pos=(207, 0), size=(100, -1))
        button = wx.Button(panel, wx.ID_ANY, pos=(320, 0), size=(80, 26), label='增加單詞')
        button.Bind(wx.EVT_BUTTON, self.get_word)
        button2 = wx.Button(panel, wx.ID_ANY, pos=(0, 200), size=(430, 30), label='查看所有單詞')
        button2.Bind(wx.EVT_BUTTON, self.scan_word)
        button3 = wx.Button(panel, wx.ID_ANY, pos=(0, 150), size=(140, 30), label='隨機學習')
        button3.Bind(wx.EVT_BUTTON, self.review_word)
        button4 = wx.Button(panel, wx.ID_ANY, pos=(145, 150), size=(140, 30), label='標記')
        button4.Bind(wx.EVT_BUTTON, self.tag_button)
        button5 = wx.Button(panel, wx.ID_ANY, pos=(290, 150), size=(140, 30), label='隨機查看標記單詞')
        button5.Bind(wx.EVT_BUTTON, self.get_taged_word)
        self.text = wx.TextCtrl(panel, -1, "", pos=(3, 70), size=(170, 50))
        self.text2 = wx.TextCtrl(panel, -1, "", pos=(200, 70), size=(170, 50))
        self.text3 = wx.TextCtrl(panel, -1, "", pos=(0, 230), size=(430, 110), style=wx.TE_MULTILINE)
        self.total_word = wx.TextCtrl(panel, -1, "", pos=(140, 27), size=(40, 25))
        self.ran = None
        self.load_file(self)
        self.get_total_word(self)
 
    def get_total_word(self, event):
        global num
        self.total_word.AppendText(str(len(num)))
 
    def write_word2file(self, Eng, ZH):
        f = open(".\dic.txt", 'a+')
        f.write(Eng + ':'+ZH + '\n')
        f.close()
 
    def get_word(self, event):
        global num
        if self.temp_Eng.GetValue() != None or self.temp_ZH.GetValue() != None:
            self.write_word2file(self.temp_Eng.GetValue(), self.temp_ZH.GetValue())
        self.temp_Eng.Clear()
        self.temp_ZH.Clear()
        self.total_word.Clear()
        self.load_file(self)
        self.total_word.AppendText(str(len(num)))
 
    def load_file(self, event):
        global mydic
        global num
        f = open("E:\\dic.txt", 'r')
        for line in f.readlines():
            if ':' in line and line[:len(line)-1] not in num:
                mydic[line[:line.index(':')]] = line[line.index(':')+1:len(line)-1]
                num.append(line[:len(line)-1])
 
    def scan_word(self, event):
        global num
        self.load_file(event)
        self.text.Clear()
        for _ in range(len(num)):
            self.text3.AppendText(num[_]+'\n')
 
 
    def review_word(self, event):
        global num
        self.text.Clear()
        self.text2.Clear()
        self.ran = random.randint(0, len(num)-1)
        while self.ran == random.randint(0, len(num)-1):
            self.ran = random.randint(0, len(num) - 1)
        self.text.AppendText(num[self.ran][:num[self.ran].index(':')])
        self.text2.AppendText(num[self.ran][num[self.ran].index(':')+1:])
 
    def tag_button(self, event):
        if self.ran != None:
            self.tag_word(self.ran)
 
    def tag_word(self, event):
        global num
        global taged
        if num[self.ran] not in taged:
            taged.append(num[self.ran])
        print(num)
 
    def get_taged_word(self, event):
        global taged
        self.text.Clear()
        self.text2.Clear()
        _ = random.randint(0, len(taged)-1)
        while _ == random.randint(0, len(taged)-1):
            _ = random.randint(0, len(taged) - 1)
        self.text.AppendText(taged[_][:taged[_].index(':')])
        self.text2.AppendText(taged[_][taged[_].index(':') + 1:])
 
class MainApp(wx.App):
    def OnInit(self):
        self.frame1 = InfoPanel(None, -1)
        self.frame1.Center()
        self.frame1.Show(True)
        self.SetTopWindow(self.frame1)
        return True
 
 
if __name__ == '__main__':
    app = MainApp(0)
    app.MainLoop()
 
 

標簽: Python
頂一下
(0)
0%
踩一下
(0)
0%

Google提供的廣告

爱彩乐十一选五 股票涨跌无限制 赌城首页 炸金花百人场什么 北京pk10赛车公式图稳赚5码 北京快乐8全包稳赚法 北京股票融资合作 彩神8快3下载 时时彩一天赚2000技巧 大乐透带坐标的走势图 沙琅开个粉皮店赚钱吗 必富备用网址 财神捕鱼app 重庆时时五星彩走势图 无网单机斗地主免费版 炸金花手机版下载免费 百度云资料赚钱