基于tkinter库python背单词GUI设计——单词越背越开心

基于tkinter库python背单词GUI设计——单词越背越开心_第1张图片

基于tkinter库python背单词GUI设计——单词越背越开心_第2张图片

根据本人背单词的习惯开发的小程序

  1. 复制单词信息:格式参考输入栏自带的内容
  2. 读取单词,按键1,按此顺序显示在LABEL上
  3. 剩下的自己探索吧

每次背单词都要打开编译软件很麻烦,可以试试tkinker打包直接生成.exe的程序直接运行;可以看看我的另一篇文章

https://blog.csdn.net/hu706789355/article/details/123006940https://blog.csdn.net/hu706789355/article/details/123006940

第一版:

#time:2022/2/14  20:26
#author:
import time
import tkinter as tk
from tkinter import ttk,messagebox as msg
import tkinter.font as tkfont
import random

##########################——————————主要全局变量——————————###############################
DISTANCE=10
DISPLAY_MAX=8
ROW=0
EN_VISIABLE=0
tran_visi_flag=0
word_count=0
dis_trans_cnt=0
word_list_origin=[]
word_list_random=[]
class WordClass:
    def __init__(self,only,all):
        self.word_only=only
        self.word_all=all
############################-----------主要函数------------#############################
def ReadInytextBox():
    global dis_trans_cnt
    dis_trans_cnt = 0
    outText.delete(1.0,'end')
    file=inText.get('1.0','end')
    #默认第一个字符就是第一个单词的开始
    global word_count
    global word_list_random
    global tran_visi_flag
    # 如果outtext里有内容就读取,没有就显示已经读取的内容
    if len(inText.get('1.0','end'))>=10:
        word_count=0
        start_i = 0
        sum_of_str = file.count('');  # 统计字符个数,‘’为空字符,即任意字符都计数
        print('字符个数总计:', sum_of_str);
        for i in range(0, sum_of_str-2, 1):
            print(i,file[i])
            if file[i]=='\n':
                j=start_i
                print('    ',i,j)
                while file[j]!='/' and file[j]!=' ':
                    print(j,' ',file[j])
                    j=j+1
                end_i=j-1
                word_count+=1
                print('word_count:',word_count)
                if len(word_list_origin)word_count or dis_trans_cnt>DISPLAY_MAX:
        dis_trans_cnt=0
        ShuffleWords()
    print(dis_trans_cnt)
    DisplayTabels(word_list_random, 'OneNextOne')

nextButton= ttk.Button(root, text='显示下一个单词的翻译', width=20, command=NextDisTrans)
nextButton.grid(row=ROW + 1, padx=DISTANCE, pady=DISTANCE, sticky=' ', ipady=10)

readButton = ttk.Button(root, text='读取/顺序显示', width=40, command=ReadInytextBox)
readButton.grid(row=ROW + 3, padx=DISTANCE, pady=DISTANCE, sticky='w', ipady=10)
ranButton = ttk.Button(root, text='随机顺序测试', width=40, command=ShuffleWords)
ranButton.grid(row=ROW + 3, padx=DISTANCE, pady=DISTANCE, sticky=' ', ipady=10)
coverButton = ttk.Button(root, text='打开/关闭翻译', width=40, command=SwitchTranslateVisity)
coverButton.grid(row=ROW + 3, padx=DISTANCE, pady=DISTANCE, sticky='e', ipady=10)

root.mainloop()

第二版,+键盘事件+单词读音+美化

#time:2022/2/14  20:26
#author:Hu_Guangliang
#
import time
import tkinter as tk
from tkinter import ttk
from tkinter import PhotoImage
import tkinter.font as tkfont
import random
from MP3_PLAYER import *
##########################——————————主要全局变量——————————###############################
tkinter_LABEL_XY_DISTANCE=10#行间距=字间距
tkinter_LABEL_LINE_MAX=8#最大显示单词数
tkinter_ROW=0#显示用的行号,递增
tkinter_mp3_BOTTONclass=[]
tkinter_LABELclass=[]
#Label
LABLE_temp_word=[]
LABEL_word=[]
STYLE_BOTTON_font=('OPPOSans R', 15, '')
STYLE_LABEL_font=('MiSans Demibold', 30, '')
STYLE_TEXT_font=('MiSans Regular', 10, '')
word_count=0
dis_trans_cnt=0


word_list_origin=[]
word_list_buffer=[]

root=tk.Tk()
root.title('背单词V20220414')
root.resizable(0,0)
#F5F6EE
#FFB000
#419FB7
#17456B
#0F1D2B
photo = tk.PhotoImage(file="image/img_play.png")

# 调整图片尺寸适应按钮大小
photoimage = photo.subsample(100, 100)

def handleKeyEvent(event):
    print(event)
    if event.keysym=='Up':
        NextDisTrans()
    if event.keysym=='Down':
        ShuffleWords()
    if event.keysym=='Left':
        ReadInytextBox()
    if event.keysym=='Right':
        SwitchTranslateVisity()
    if event.keysym=='Return':
        mp3_P(dis_trans_cnt)


# 将主窗口与键盘事件连接
eventType = ["Key", "Control-Up", "Return", "Escape", "F1", "F2", "F3", "F4",
             "F5", "F6", "F7", "F8", "F9", "F13", "F11", "F12", "Num_Lock", "Scroll_Lock",
             "Caps_Lock", "Print", "Insert", "Delete", "Pause", "Prior", "Next", "BackSpace",
             "Tab", "Cancel", "Control_L", "Alt_L", "Shift_L", "End", "Home", "Up", "Down",
             "Left", "Right"]
for type in eventType:
    root.bind("<" + type + ">", handleKeyEvent)



class WordClass:
    def __init__(self,only,all):
        self.word_only=only
        self.word_all=all
class BOTTON_CLASS():
    def __init__(self,data):
        self.num=data
        self.botton_class=tk.Button(root, image=photo,text='', bg='#F5F6EE',width=50,height=50,relief='flat',command=self.play_class,)#
    def play_class(self):
        mp3_P(self.num)
############################-----------主要函数------------#############################
def ReadInytextBox():
    global dis_trans_cnt
    outText.delete(1.0,'end')
    file=inText.get('1.0','end')
    #默认第一个字符就是第一个单词的开始
    global word_count
    global word_list_buffer

    # 如果outtext里有内容就读取,没有就显示已经读取的内容
    if len(inText.get('1.0','end'))>=10:
        word_count=0
        start_i = 0
        sum_of_str = file.count('');  # 统计字符个数,‘’为空字符,即任意字符都计数
        # print('字符个数总计:', sum_of_str);
        for i in range(0, sum_of_str-2, 1):
            # print(i,file[i])
            if file[i]=='\n':
                j=start_i
                # print('    ',i,j)
                while file[j]!='/' and file[j]!=' ':
                    # print(j,' ',file[j])
                    j=j+1
                end_i=j-1
                word_count+=1
                # print('word_count:',word_count)
                if len(word_list_origin)word_count or dis_trans_cnt>tkinter_LABEL_LINE_MAX:
        dis_trans_cnt=0
        ShuffleWords()
    DisplayTabels(word_list_buffer, dis_trans_cnt)

def mp3_P(data_int):
    print(data_int)
    try:
        mp3_player(word_list_buffer[data_int].word_only)
    except:
        outText.insert(0.0, "没有音频文件")
        pass
##########################——————————初始化——————————##################################


for i in range(0, tkinter_LABEL_LINE_MAX):

    LABEL_word.append('word: void')
    LABLE_temp_word.append(tk.StringVar())
    LABLE_temp_word[i].set(LABEL_word[i])
    # ft_label = tkfont.Font(family='MiSans Demibold', size=30, weight=tkfont.NORMAL)
    tkinter_LABELclass.append(tk.Label(root, textvariable=LABLE_temp_word[i], font=STYLE_LABEL_font, fg='#0F1D2B',bg='#F5F6EE', justify='left', anchor='w', padx=100, width=40, relief='raised', borderwidth=0))
    tkinter_LABELclass[i].grid(row=tkinter_ROW, padx=0, ipady=10)
    tkinter_mp3_BOTTONclass.append(BOTTON_CLASS(i))
    tkinter_mp3_BOTTONclass[i].botton_class.grid(row=tkinter_ROW, padx=10, pady=tkinter_LABEL_XY_DISTANCE, sticky='w', ipady=0)
    tkinter_ROW += 1
# # 输入窗口


inText = tk.Text(root, height=10, width=40,font=STYLE_TEXT_font, )
inText.insert(0.0,
"accessory / æk’sesәri/n.配件、同谋 a.附属的\n"
"accommodate / ә’kɔmәdeit/vt.容纳;供应,供给\n"
"accord / ә’kɔ:d/n.条约vt.与…一致(~ with);给予\n"
"acknowledge / әk’nɔlidʒ/vt.承认;告知收到\n"
"acquaint / ә’kweint/vt.使认识,使了解\n"
"acquisition / ‘ækwi’ziʃәn/n. 收获\n"
"activate / ‘æktiveit/vt. 刺激,使活动\n")
inText.grid(row=tkinter_ROW + 1, padx=tkinter_LABEL_XY_DISTANCE, pady=tkinter_LABEL_XY_DISTANCE, sticky='w')

# 输出窗口
outText = tk.Text(root, height=10, width=40,font=STYLE_TEXT_font, )
outText.insert(0.0, "输出单词567890")
outText.grid(row=tkinter_ROW + 1, padx=tkinter_LABEL_XY_DISTANCE, pady=tkinter_LABEL_XY_DISTANCE, sticky='e')
# 设置 tag(暂时没用)
# outText.tag_config("tag_1", backgroun="yellow", foreground="red")
# ft=tkfont.Font(family='微软雅黑',size=10)
# outText.configure(ft)
# # outText.tag_add('tag_1',a1)


nextButton= tk.Button(root, text='显示下一个单词的翻译', width=20, bg='#419FB7',command=NextDisTrans,relief='ridge',font=STYLE_BOTTON_font)
nextButton.grid(row=tkinter_ROW + 1, padx=tkinter_LABEL_XY_DISTANCE, pady=tkinter_LABEL_XY_DISTANCE, sticky=' ', ipady=10)



readButton = tk.Button(root, text='读取/顺序显示', width=20, bg='#419FB7',command=ReadInytextBox,relief='ridge',font=STYLE_BOTTON_font)
readButton.grid(row=tkinter_ROW + 3, padx=tkinter_LABEL_XY_DISTANCE, pady=tkinter_LABEL_XY_DISTANCE, sticky='w', ipady=10)
ranButton = tk.Button(root, text='随机顺序测试', width=20, bg='#419FB7',command=ShuffleWords,relief='ridge',font=STYLE_BOTTON_font)
ranButton.grid(row=tkinter_ROW + 3, padx=tkinter_LABEL_XY_DISTANCE, pady=tkinter_LABEL_XY_DISTANCE, sticky=' ', ipady=10)
coverButton = tk.Button(root, text='打开/关闭翻译', width=20, bg='#419FB7',command=SwitchTranslateVisity,relief='ridge',font=STYLE_BOTTON_font)
coverButton.grid(row=tkinter_ROW + 3, padx=tkinter_LABEL_XY_DISTANCE, pady=tkinter_LABEL_XY_DISTANCE, sticky='e', ipady=10)
print(player.get_volume())
player.set_volume(100)
print(player.get_volume())
#
# sss=tk.Scrollbar(root,orient='',background='red',width=100,borderwidth=100)
# sss.grid(row=tkinter_ROW + 3, pady=tkinter_LABEL_XY_DISTANCE)
#F5F6EE
#FFB000
#419FB7
#17456B
#0F1D2B

root.mainloop()

你可能感兴趣的:(python,开发语言)