Python使用Tkinter简单实现查英文单词小程序!

代码比较简单,直接上代码,可作为Tkinter入门demo!

源码

import requests
from tkinter import *

class Window:
        def __init__(self, window):
            self.window = window
        # 设置窗口
        def set_init_window(self):
            self.window.title("English_Query")  # 窗口名
            self.window.geometry('500x300')
            # self.window["bg"] = "pink"  # 窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
            self.window.attributes("-alpha", 0.9)  # 虚化,值越小虚化程度越高
            # 标签
            self.input_lable = Label(self.window,text="请输入需要查询的单词").place(x=10, y=22)
            self.result_lable = Label(self.window, text="以下为查询结果").place(x=10, y=110)

            # 文本框
            self.input_text = Text(self.window,  width=30, height=2) # 原始数据录入框
            self.input_text.place(x=140, y=20)
            self.result_text= Text(self.window,  width=60, height=8, xscrollcommand=1)
            self.result_text.place(x=20, y=140)

            # 按钮
            self.query_button = Button(self.window, text="查询", bg="lightblue", width=10, command=self.query).place(x=200, y=80)  # 调用内部方法  加()为直接调用
        def query(self):
            word = self.input_text.get(1.0, END).strip().replace("\n", "").encode()
            if len(word) == 0:
                self.result_text.insert(1.0, "非法查询")
                return

            url = "https://fanyi.baidu.com/sug"
            data = {
                "kw": word
            }
            response = requests.post(url, data=data)
            response.encoding = "utf-8"
            result = response.json()
            datalist = result['data']
            self.result_text.delete(1.0, END)
            for dir in reversed(datalist):
                self.result_text.insert(1.0, "单词为{:s}  释义为{:s}\n".format(dir['k'], dir['v']))
                # print("单词为{:s}  释义为{:s}".format(dir['k'], dir['v']))

window = Tk()
gui = Window(window)
gui.set_init_window()
window.mainloop()

 

 

运行界面

 

微信关注

WeChat

阅读剩余
THE END