tkinter多页面小程序
test1.py
# -*- coding:utf-8 -*-
import tkinter
from firstpage import *
root = tkinter.Tk()
root.title('学习小霸王')
firstpage(root)
root.mainloop()
firstpage.py
# -*- coding:utf-8 -*-
import tkinter
from tkinter.messagebox import *
import test3
from PIL import ImageTk, Image
class firstpage(object):
def __init__(self, master=None):
self.root = master
self.image2 = Image.open(r'/home/allen/photo.jpg')
self.background_image = ImageTk.PhotoImage(self.image2)
self.w = self.background_image.width()
self.h = self.background_image.height()
self.root.geometry('%dx%d+0+0' % (self.w, self.h))
self.background_label = tkinter.Label(self.root, image=self.background_image)
self.background_label.place(x=0, y=0, relwidth=1, relheight=1)
self.choose()
def choose(self):
#self.page = tkinter.Frame(self.root)
#self.page.pack()
#tkinter.Label(self.page).grid(row=0, stick='W')
chibut = tkinter.Button(self.root, text='学习成语', command=self.gochi)
chibut.grid(row=3, stick='W', padx=200, pady=150, rowspan=3, columnspan=3)
chibut.config(font=("宋体", 20))
engbut = tkinter.Button(self.root, text='学习英语', command=self.goeng)
engbut.grid(row=3, column=1, stick='E', rowspan=3, columnspan=3)
engbut.config(font=("宋体", 20))
quitbut = tkinter.Button(self.root, text='退出', command=self.root.quit)
quitbut.grid(row=5, column=1, stick='E', rowspan=3, columnspan=3)
quitbut.config(font=("宋体", 20))
def gochi(self):
#self.page.destroy()
test3.learnchi(self.w, self.h, self.root)
def goeng(self):
#self.page.destroy()
test3.learneng(self.w, self.h, self.root)
test3.py
# -*- coding: utf-8 -*-
import tkinter
import time
import threading
import tkinter.messagebox
from PIL import ImageTk,Image
import test2
from aip import AipSpeech
import os
class learnchi(object):
def __init__(self, w, h, master=None):
self.top = tkinter.Toplevel()
#self.root = master
self.top.geometry('%dx%d+0+0' % (w, h))
self.image2 = Image.open(r'/home/allen/photo.jpg')
self.background_image = ImageTk.PhotoImage(self.image2)
self.background_label = tkinter.Label(self.top, image=self.background_image)
self.background_label.place(x=0, y=0, relwidth=1, relheight=1)
self.words_prom = tkinter.Label(self.top, text='请输入成语:', font=("宋体", 15))
self.words_prom.place(x=100, y=100, width=200, height=50)
self.words = tkinter.Entry(self.top, font=("宋体", 20))
self.words.place(x=310, y=100, width=200, height=50)
self.expl_prom = tkinter.Label(self.top, text='成语的意思是:', font=("宋体", 15))
self.expl_prom.place(x=100, y=200, width=200, height=50)
self.btn_start = tkinter.Button(self.top, text='确定', command=self.get_explain, bg='gold')
self.btn_start.focus_set()
self.btn_start.pack()
self.btn_start.place(x=200, y=550, width=150, height=50)
self.btn_start.config(font=("Courier", 20))
self.btn_quit = tkinter.Button(self.top, text='退出', command=self.top.destroy, bg='gold')
self.btn_quit.pack()
self.btn_quit.place(x=600, y=550, width=150, height=50)
self.btn_quit.config(font=("Courier", 20))
self.APP_ID = '*********'
self.API_KEY = '4S****************************'
self.SECRET_KEY = 'YG7L**************************'
self.client = AipSpeech(self.APP_ID, self.API_KEY, self.SECRET_KEY)
#self.get_explain()
def get_explain(self):
t = threading.Thread(target=self.rounds)
t.start()
def rounds(self):
get_words = self.words.get()
expl = test2.get_expl(get_words)
expl_content = tkinter.Text(self.top, width=40, height=8, font=("宋体", 15))
expl_content.place(x=100, y=260)
expl_content.insert(1.0, expl)
text = "您输入的成语是" + "。" + get_words + "。" + "这个成语的意思是" + "。" + expl
result = self.client.synthesis(text, 'zh', 1, {
'sdp': 5, 'vol': 5
})
# 识别正确返回语音二进制 错误则返回dict 参照下面错误码
if not isinstance(result, dict):
with open('audio.mp3', 'wb') as f:
f.write(result)
os.system("/usr/bin/totem /root/audio.mp3")
import wordslib1
def get_expl(get_words):
while True:
if get_words in wordslib1.wordsall.keys():
explain = wordslib1.wordsall[get_words]
break
else:
get_words = input("你输入了一个错误的成语,请重新输入:")
return explain