我正在使用python和PyQt5构建此gui词典应用程序。 data.json 是包含键及其定义的文件(数据的排列方式类似于python字典)。
dictionary.ui是我在 pyqt5 Designer应用程序
中构建的UI(小部件类=“ QMainWindow ” name =“ MainWindow”)。 (小部件类=“ QPushButton” name =“ pushButton ”)。 (widget class =“ QLineEdit” name =“ lineEdit ”)。(widget class =“ QLabel” name =“ resultArea ”)
程序显示gui后会运行。但是,当我输入内容时,程序崩溃而无输出。我在修复此代码时遇到问题。
问题出在这行代码 self.resultArea.setText(self.trasnlate(self.lineEdit.text()))
错误为Translate is a method not a class instance
。
我需要在 lineEdit 标签中输入的用户输入作为 translate()方法的参数,并在 resultArea 中显示该方法的结果如果单击按钮,则为strong>标签。
import json
from difflib import get_close_matches
import sys
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QApplication,QMainWindow
from PyQt5.uic import loadUi
data = json.load(open("data.json"))
class DictionaryApp(QMainWindow) :
def __init__(self) :
super(DictionaryApp,self).__init__()
loadUi('dictionary.ui',self)
self.setWindowTitle('Dictionary')
self.pushButton.clicked.connect(self.on_pushButton_clicked)
@pyqtSlot()
def on_pushButton_clicked(self) :
self.resultArea.setText(self.trasnlate(self.lineEdit.text()))
@staticmethod
def translate(w) :
w = w.lower()
if w in data :
return data[w]
elif w.title() in data:
return data[w.title()]
elif w.upper() in data:
return data[w.upper()]
elif len(get_close_matches(w, data.keys())) > 0 :
YN = input("Did you mean {} instead? Enter 'y' for yes, 'n' for no: ".format(get_close_matches(w, data.keys())[0])).lower()
if YN == "y" :
return data[get_close_matches(w, data.keys())[0]]
elif YN == "n" :
return "The word doesn't exist. Please double check it. "
else :
return "Wrong entry. "
else :
return "The word doesn't exist. Please double check it. "
app=QApplication(sys.argv)
widget=DictionaryApp()
widget.show()
sys.exit(app.exec_())