我的新关键字“stackoverflow”有以下对话框:
按下“hinzufügen”按钮后,将调用以下方法:
keywords = self.wort_neue_woerter.toPlainText()
try:
with open("files/schluesselwoerter.txt", 'r') as file:
output = file.read()
self.keywords += output
except:
print("Datei nicht gefunden")
with open("files/schluesselwoerter.txt", 'w') as file:
file.write(self.keywords)
print("Ausgeführt")
每个关键字都写在“schlüsselwoerter.txt”文件中:
第一个对话框中的方法使用此文本文件填充ListWidget。
with open('files/schluesselwoerter.txt', 'r') as file:
output = file.read()
output_list = output.split(',')
for element in output_list:
item = QtWidgets.QListWidgetItem(element.strip())
self.wort_uebersicht.addItem(item)
我想在按下对话框2中的按钮后立即调用Dialog 1方法populate_listWidget
。我该怎么做?
以下是完整代码:
主类:
import sys
import main_window
from PyQt5 import QtWidgets
class GUI(QtWidgets.QMainWindow, main_window.Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
gui = GUI()
gui.show()
sys.exit(app.exec_())
Main_Window:
from PyQt5 import QtCore, QtGui, QtWidgets
import dialog
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(800, 600)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.listWidget.setObjectName("listWidget")
self.horizontalLayout.addWidget(self.listWidget)
self.main_insert_btn = QtWidgets.QPushButton(self.centralwidget)
self.main_insert_btn.setObjectName("main_insert_btn")
self.horizontalLayout.addWidget(self.main_insert_btn)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 800, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
try:
self.populate_list()
except:
print("Population failed")
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
self.main_insert_btn.clicked.connect(self.create_new_keywords)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.main_insert_btn.setText(_translate("MainWindow", "Insert"))
def create_new_keywords(self):
keyword_dialog = QtWidgets.QDialog()
keyword_dialog.ui = dialog.Ui_Dialog()
keyword_dialog.ui.setupUi(keyword_dialog)
keyword_dialog.exec_()
def populate_list(self):
with open('schluesselwoerter.txt', 'r') as file:
output = file.read()
output_list = output.split(',')
for element in output_list:
item = QtWidgets.QListWidgetItem(element.strip())
self.listWidget.addItem(item)
插入对话框:
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
Dialog.setObjectName("Dialog")
Dialog.resize(400, 300)
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.verticalLayout.setObjectName("verticalLayout")
self.text_input = QtWidgets.QPlainTextEdit(Dialog)
self.text_input.setObjectName("text_input")
self.verticalLayout.addWidget(self.text_input)
self.insert_btn = QtWidgets.QPushButton(Dialog)
self.insert_btn.setObjectName("insert_btn")
self.verticalLayout.addWidget(self.insert_btn)
self.retranslateUi(Dialog)
QtCore.QMetaObject.connectSlotsByName(Dialog)
self.insert_btn.clicked.connect(self.create_new_entry)
def retranslateUi(self, Dialog):
_translate = QtCore.QCoreApplication.translate
Dialog.setWindowTitle(_translate("Dialog", "Dialog"))
self.insert_btn.setText(_translate("Dialog", "Insert"))
def create_new_entry(self):
keywords = self.text_input.toPlainText()
try:
with open("schluesselwoerter.txt", 'r') as file:
output = file.read()
keywords += output
except:
print("Datei nicht gefunden")
with open("schluesselwoerter.txt", 'w') as file:
file.write(keywords)
print("Ausgeführt")
self.text_input.setPlainText("")
答案 0 :(得分:1)
您只想在继承这两个类(insert-dialog
和QDialog
)的同时创建Ui_Dialog
,就像在GUI
类中所做的那样:
from PyQt5 import QtWidgets
class Ui_Dialog(object):
def setupUi(self, Dialog):
self.verticalLayout = QtWidgets.QVBoxLayout(Dialog)
self.text_input = QtWidgets.QPlainTextEdit(Dialog)
self.verticalLayout.addWidget(self.text_input)
self.insert_btn = QtWidgets.QPushButton('add', Dialog)
self.verticalLayout.addWidget(self.insert_btn)
class InsertDialog(QtWidgets.QDialog, Ui_Dialog):
def __init__(self, parent = None):
super(InsertDialog, self).__init__(parent)
self.setupUi(self)
self.insert_btn.clicked.connect(self.create_new_entry)
def create_new_entry(self):
keywords = self.text_input.toPlainText()
try:
with open("schluesselwoerter.txt", 'r') as file:
output = file.read()
output += (',' + keywords) # add new word to end of list
except:
print("File not found")
with open("schluesselwoerter.txt", 'w') as file:
file.write(output)
print("done")
self.text_input.setPlainText("")
为了提供一个最小的示例,在编辑后,其余代码如下所示:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.listWidget = QtWidgets.QListWidget(self.centralwidget)
self.horizontalLayout.addWidget(self.listWidget)
self.main_insert_btn = QtWidgets.QPushButton('Insert', self.centralwidget)
self.horizontalLayout.addWidget(self.main_insert_btn)
MainWindow.setCentralWidget(self.centralwidget)
class GUI(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super(GUI, self).__init__()
self.setupUi(self)
self.populate_list()
self.main_insert_btn.clicked.connect(self.create_new_keywords) # do this outside
def create_new_keywords(self):
insert_dialog = InsertDialog(self)
insert_dialog.exec()
self.populate_list() # refresh list, after you are done
def populate_list(self):
try:
with open('schluesselwoerter.txt', 'r') as file:
output = file.read()
output_list = output.split(',')
self.listWidget.clear() # to prevent double entries
for element in output_list:
item = QtWidgets.QListWidgetItem(element.strip())
self.listWidget.addItem(item)
except:
print("Population failed")
if __name__ == '__main__':
[...]
旁注:我非常喜欢德语 - 如果您希望有人帮助您,我建议您用英语提供整个问题。翻译它并没有太大的努力,特别是当你请求帮助时(免费!)。
请记住,不是每个人都喜欢这个国家 - 也更有礼貌。