美文网首页
python 图形界面单词app

python 图形界面单词app

作者: Trade丿Life | 来源:发表于2019-08-25 17:02 被阅读0次

从0到1按照自己的设计思路设计的单词app,自己平时不会的单词可以记录下来放入excel,读取excel里的单词形成形成列表,随机产生单词,通过键盘输入意思判断对错,虽然要多点几下按钮,第一次设计不喜勿喷。

1.思路

需要python自带的tkinter库,需要下载xlrd库读取excel内容

建立读取excel内容并形成列表,再根据对应的中英文构造字典

用tkinter建立图形窗口,把上一步建立的函数和按钮建立响应

2.上代码

import xlrd
import random
from tkinter import *
class Word():
    name = ""
    mean = ""
    def getword(path):
        list = []
        data = xlrd.open_workbook(path)
        # 通过索引获取
        table1 = data.sheets()[0]
        # 获取行和列
        nrows = table1.nrows
        ncols = table1.ncols
        rows = table1.row_values(0)
        cols = table1.col_values(0)
        item = table1.cell_value(0, 0)
        for i in range(5):
            item1 = table1.cell_value(i, 0)
            list.append(item1)
        random.shuffle(list)
        return list

    def constructDict(list,txt):
        global newword
        global dict
        txt.delete(0.0,END)#删除文本内容
        random.shuffle(list)
        dict = {}
        li = []
        for i in list:
            #把单词和意思填充到字典
            dict[i.split(":")[0]] = i.split(":")[1]
            li.append(i.split(":")[0])
        newword = li[random.randint(0,4)]
        txt.insert(END, newword)#文本框里放入随机产生的单词
        return dict,newword
    def Gui():

        list = Word.getword('C:\\Users\\空雨衣\\Downloads\\word.xls')
        def run2(newword):
            txt1.delete(0.0, END)
            mean = inp2.get()
            print(mean)
            print(1)
            if mean == dict[newword]:
                txt1.insert(END, 'right')
            else:
                txt1.insert(END, '猪')
             # 清空输入
            inp2.delete(0, END)  # 清空输入

        root = Tk()#创建一个窗口
        root.geometry('460x240')
        root.title('单词app')#创建一个标题

        lb1 = Label(root, text='Please input a meaning')
        lb1.place(relx=0.35, rely=0.5, relwidth=0.8, relheight=0.1)

        txt = Text(root)#创建一个显示单词的文本
        txt.place(rely=0.1, relheight=0.3)#显示创建的文本

        inp2 = Entry(root)#创建输入框控件
        inp2.place(relx=0.6, rely=0.2, relwidth=0.3, relheight=0.1)

        btn1 = Button(root, text='nextword', command=lambda:Word.constructDict(list,txt))#创建一个按钮控件和单词字典的函数建立响应
        btn1.place(relx=0.1, rely=0.42, relwidth=0.3, relheight=0.1,)
        dict,newword = Word.constructDict(list,txt)
        txt1 = Text(root)#创建显判断单词是否正确的文本框
        txt1.place(rely=0.6, relheight=0.4)
        btn1 = Button(root, text='Click', command=lambda:run2(newword))# 带参数的comman需要用lambda表达式
        btn1.place(relx=0.6, rely=0.4, relwidth=0.3, relheight=0.1)
        root.mainloop()
Word.Gui()

3.效果

单词app.JPG

4.小结

当一个函数调用另一个函数时,若被调用的函数有调用函数的参数,需要在调用函数上把参数传到被调用函数。

总算自己写出一个小应用开心,果然散步可以帮助产生灵感一点不假。

相关文章

网友评论

      本文标题:python 图形界面单词app

      本文链接:https://www.haomeiwen.com/subject/unfkectx.html