不使用数据库,实现一个简单的记生词软件,基本功能包括,添加新的生词及其中文含义,浏览已经记录的单词,随机选择部分单词进行复习。可考虑其它拓展的功能。
这里使用wxpython的基本操作,以及类与函数的调用方式。
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() |
[…] ③wxpython简单记录生词GUI程序 […]