- 簡介:本文主要介紹如何用 Python 內置的 tkinter 寫一個查詢工具,
-
很多人學習python,不知道從何學起,
很多人學習python,掌握了基本語法過后,不知道在哪里尋找案例上手,
很多已經做案例的人,卻不知道如何去學習更加高深的知識,
那么針對這三類人,我給大家提供一個好的學習平臺,免費領取視頻教程,電子書籍,以及課程的源代碼!
QQ群:961562169
準備
- 環境
- Windows 10
- Python 3.7.3
- VS Code
- 依賴
- tkinter
- python-Levenshtein
- fuzzywuzzy
目錄結構及運行界面

?
圖1 目錄結構圖

?
圖2 查詢界面圖

?
圖3 查詢結果圖
具體實作
import tkinter as tk
from tkinter import *
from tkinter import ttk
from tkinter import messagebox
from fuzzywuzzy import fuzz
class Window(object):
def __init__(self):
root = tk.Tk()
root.minsize(580, 320) # 視窗大小
root.resizable(width=False, height=False) # False視窗大小不可變
# root.iconbitmap('data/icon.ico') # 無法顯示圖片
root.title('魯迅:都是我說的?!') # 視窗標題
label1 = Label(text='句子:') # 標簽
label1.place(x=10, y=10, width=80, height=25) # 確定位置
self.line_text = Entry(root) # 單行文本輸入
self.line_text.place(x=80, y=10, width=300, height=25)
button = Button(text='開始查詢', command=self.inquiry) # 按鈕
button.place(x=390, y=10, width=60, height=25)
self.filemenu = tk.StringVar() # 下拉串列
self.file_menu = ttk.Combobox(root, width=12, textvariable=self.filemenu)
# 串列內容
self.file_menu['values'] = ('匹配度: 100%', '匹配度: 90%',
'匹配度: 80%', '匹配度: 70%')
self.file_menu.place(x=460, y=10, width=100, height=25)
self.file_menu.current(0) # 當前顯示 100%
label2 = Label(text='查詢結果:')
label2.place(x=10, y=100, width=80, height=20)
self.text = Text(root) # 多行文本顯示
self.text.place(x=80, y=50, width=480, height=240)
self.paragraphs = self.load_data('data/book.txt') # 資料檔案
root.mainloop() # 主回圈
'''查詢'''
def inquiry(self):
sentence = self.line_text.get() # 獲取輸入的內容
matched = []
score_thresh = self.get_score_thresh()
self.text.delete(1.0, tk.END) # 用于洗掉后續顯示的檔案
if not sentence: # 沒有輸入句子就查詢,會出現彈窗警告
messagebox.showinfo("Warning", '請先輸入需要查詢的魯迅名言')
else:
for p in self.paragraphs:
score = fuzz.partial_ratio(p, sentence)
if score >= score_thresh and len(sentence) <= len(p):
matched.append([score, p])
infos = []
# 查找相匹配的內容
for match in matched:
infos.append('[匹配度]: %d\n[內容]: %s\n' % (match[0], match[1]))
if not infos:
infos.append('未匹配到任何相似度大于或等于%d的句子,請修改匹配度.\n' % score_thresh)
# 查找到的內容插入文本,并顯示
self.text.insert('insert', '\n\n\n'.join(infos)[:-1])
'''根據下拉串列獲取匹配度'''
def get_score_thresh(self):
if self.file_menu.current() == 0:
return 100
elif self.file_menu.current() == 1:
return 90
elif self.file_menu.current() == 2:
return 80
elif self.file_menu.current() == 3:
return 70
'''資料匯入'''
def load_data(self, data_path):
paragraphs = []
with open(data_path, 'r', encoding='utf-8') as f:
for line in f.readlines():
if line.strip():
paragraphs.append(line.strip('\n'))
return paragraphs
if __name__ == '__main__':
Window()
小結
- 本文借鑒地址:https://github.com/CharlesPikachu/Tools/tree/master/luxunSentencesQuery 此地址代碼基于 PyQt5 模塊,Python 版本為 3.6,由于我的 Python 版本為3.7,兼容存在問題,所以我用tkinter 模塊改寫了代碼,原地址教程很詳細,感興趣,可以嘗試 PyQt5,
- 以魯迅全集為例,通過一句話,查詢是否為魯迅所說,您可以替換成自己想要查詢的人物,或者其他資料,
- 小問題
- 視窗默認圖示為羽毛,這里想要設定自定義圖示,通過以下代碼
root.iconbitmap('data/icon.ico'),并不能成功,修改圖片格式為 .jpg 仍不能成功,各位如能解決,勞煩告知,謝謝,
- 視窗默認圖示為羽毛,這里想要設定自定義圖示,通過以下代碼
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/119920.html
標籤:Python
