Python使用Tkinter实现查网课答案桌面应用!
这是我很久之前写的一个查询网课题目答案的桌面程序,主要用到了Tkinter实现UI界面,requests对接题库接口,以及打印日志、检测软件版本功能的实现。代码不多,不是太难,直接看code吧!
源码
from tkinter import *
import requests
import time
import queue
import re
LOG_LINE_NUM = 0
class MY_CHECK():
def __init__(self, init_window_name):
self.init_window_name = init_window_name
def tip(self):
self.init_window_name.title("升级") # 窗口名
self.init_window_name.geometry('200x80')
self.tip_label = Label(self.init_window_name, text="软件已经更新\n联系QQ2410685763获取最新版本\n推荐微信关注:编程那点事儿", fg='red')
self.tip_label.grid()
class MY_TIP():
def __init__(self, init_window_name):
self.init_window_name = init_window_name
def tip(self):
self.init_window_name.title("提示") # 窗口名
self.init_window_name.geometry('200x80')
self.tip_label = Label(self.init_window_name, text="欢迎您使用网课查题软件\n遇到问题联系QQ2410685763\n推荐微信关注:编程那点事儿", fg='red')
self.tip_label.grid()
class MY_GUI():
def __init__(self, init_window_name):
self.init_window_name = init_window_name
# 设置窗口
def set_init_window(self):
self.init_window_name.title("网课查题--By丶理想") # 窗口名
# self.init_window_name.geometry('320x160+10+10') #290 160为窗口大小,+10 +10 定义窗口弹出时的默认展示位置
self.init_window_name.geometry('500x340')
self.init_window_name["bg"] = "white" # 窗口背景色,其他背景色见:blog.csdn.net/chl0000/article/details/7657887
# self.init_window_name.attributes("-alpha",0.9) #虚化,值越小虚化程度越高
# 标签
self.init_data_label = Label(self.init_window_name, text="输入问题:")
self.init_data_label.grid(row=0, column=0)
self.result_data_label = Label(self.init_window_name, text="查询结果:")
self.result_data_label.grid(row=11, column=0)
self.log_label = Label(self.init_window_name, text="程序日志:")
self.log_label.grid(row=20, column=0)
# 文本框
self.init_data_Text = Text(self.init_window_name, width=60, height=2) # 原始数据录入框
self.init_data_Text.grid(row=0, column=2, rowspan=5, columnspan=10)
self.result_data_Text = Text(self.init_window_name, width=60, height=5) # 处理结果展示
self.result_data_Text.grid(row=15, column=2, rowspan=5, columnspan=10)
self.log_data_Text = Text(self.init_window_name, width=60, height=10) # 日志框
self.log_data_Text.grid(row=30, column=2, rowspan=20, columnspan=20)
# 按钮
self.str_trans_to_md5_button = Button(self.init_window_name, text="点击查询", bg="pink", width=10,
command=self.query) # 调用内部方法 加()为直接调用
self.str_trans_to_md5_button.grid(row=70, column=6, rowspan=40)
# 功能函数
def query(self):
# src = self.init_data_Text.get(1.0,END).strip().replace("\n","").encode()
question = self.init_data_Text.get(1.0, END)
# print("question =", question)
da = ""
if len(question) > 2:
try:
response = requests.get(
# 此处是填写题库接口
"https://www.imyjs.cn" + question)
code = response.json()['code']
if code == "200":
tm = response.json()['tm']
answer = response.json()['da']
jx = response.json()['jx']
if len(jx) < 2:
jx = "此题暂时无解析"
da = "题目:" + tm + "\n" + "答案:" + answer + "\n" + "解析:" + jx + "\n\n" + "遇到问题联系QQ2410685763"
self.result_data_Text.delete(1.0, END)
self.result_data_Text.insert(1.0, da)
self.write_log_to_Text("INFO:Query success\n" + "题目:" + tm + "\n答案:" + answer + '\n' + "*" * 50)
else:
self.log_data_Text = Text(self.init_window_name, width=60, height=10, fg='red') # 日志框
self.log_data_Text.grid(row=30, column=2, rowspan=20, columnspan=20)
self.result_data_Text.insert(1.0, "连接服务器出错返回code=" + code)
self.write_log_to_Text("ERROE:Query failed\t" + "返回code:" + code)
except:
self.result_data_Text.delete(1.0, END)
self.result_data_Text.insert(1.0, "查询出错,未知错误")
else:
self.log_data_Text = Text(self.init_window_name, width=60, height=10, fg='red') # 日志框
self.log_data_Text.grid(row=30, column=2, rowspan=20, columnspan=20)
self.write_log_to_Text("ERROR:Query failed\t" + "查询内容有误")
# 获取当前时间
def get_current_time(self):
current_time = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
return current_time
# 日志动态打印
def write_log_to_Text(self, logmsg):
global LOG_LINE_NUM
current_time = self.get_current_time()
logmsg_in = str(current_time) + " " + str(logmsg) + "\n" # 换行
if LOG_LINE_NUM <= 10:
self.log_data_Text.insert(END, logmsg_in)
LOG_LINE_NUM = LOG_LINE_NUM + 1
else:
self.log_data_Text.delete(1.0, 2.0)
self.log_data_Text.insert(END, logmsg_in)
def check():
tip_window = Tk()
TIP = MY_CHECK(tip_window)
TIP.tip()
tip_window.mainloop()
tip_window.quit()
def run_gui():
init_window = Tk() # 实例化出一个父窗口
YJS_FIRST = MY_GUI(init_window)
# 设置根窗口默认属性
YJS_FIRST.set_init_window()
init_window.mainloop() # 父窗口进入事件循环,可以理解为保持窗口运行,否则界面不展示
def run_tip():
tip_window = Tk()
TIP = MY_TIP(tip_window)
TIP.tip()
tip_window.mainloop()
def get_mid_str(s, start_str, stop_str):
# 查找左边文本的结束位置
start_pos = s.find(start_str)
if start_pos == -1:
return None
start_pos += len(start_str)
# 查找右边文本的起始位置
stop_pos = s.find(stop_str, start_pos)
if stop_pos == -1:
return None
# 通过切片取出中间的文本
return s[start_pos:stop_pos]
# 失效方法
def check_version():
url = ""
header = {
}
response = requests.get(url, headers=header)
website = response.text
version = get_mid_str(website, '[edition]=', '[/edition]')
if version != '2.0.1' or version is None:
return 0
else:
return 1
version = check_version()
if version == 1:
run_tip()
run_gui()
else:
check()
运行界面
微信关注
阅读剩余
版权声明:
作者:理想
链接:https://www.imyjs.cn/archives/631
文章版权归作者所有,未经允许请勿转载。
THE END