从 PyQt GUI 类外部访问 GUI 元素 text()

编程入门 行业动态 更新时间:2024-10-20 21:05:38
本文介绍了从 PyQt GUI 类外部访问 GUI 元素 text()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

Ui_MainWindow 是由设计器和 pyuic 生成的 .py 文件,我想将 PyQt GUI 元素文本值传递给另一个文件并执行一些基本操作并返回结果.

Ui_MainWindow are .py files generated by designer and pyuic, I wanted to pass the PyQt GUI elements text values to another file and do some basic operation and return the result.

父文件

from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: _fromUtf8 = lambda s: s class Ui_MainWindow(object): def setupUi(self, MainWindow): MainWindow.setObjectName(_fromUtf8("MainWindow")) MainWindow.resize(742, 515) self.centralwidget = QtGui.QWidget(MainWindow) self.centralwidget.setObjectName(_fromUtf8("centralwidget")) self.layoutWidget = QtGui.QWidget(self.centralwidget) self.layoutWidget.setGeometry(QtCore.QRect(70, 30, 601, 331)) self.layoutWidget.setObjectName(_fromUtf8("layoutWidget")) self.gridLayout_2 = QtGui.QGridLayout(self.layoutWidget) self.gridLayout_2.setMargin(0) self.gridLayout_2.setObjectName(_fromUtf8("gridLayout_2")) self.gridLayout = QtGui.QGridLayout() self.gridLayout.setObjectName(_fromUtf8("gridLayout")) self.horizontalLayout = QtGui.QHBoxLayout() self.horizontalLayout.setObjectName(_fromUtf8("horizontalLayout")) self.label = QtGui.QLabel(self.layoutWidget) self.label.setObjectName(_fromUtf8("label")) self.horizontalLayout.addWidget(self.label) self.lineEdit = QtGui.QLineEdit(self.layoutWidget) self.lineEdit.setObjectName(_fromUtf8("lineEdit")) self.horizontalLayout.addWidget(self.lineEdit) self.gridLayout.addLayout(self.horizontalLayout, 0, 0, 1, 1) self.horizontalLayout_2 = QtGui.QHBoxLayout() self.horizontalLayout_2.setObjectName(_fromUtf8("horizontalLayout_2")) self.label_2 = QtGui.QLabel(self.layoutWidget) self.label_2.setObjectName(_fromUtf8("label_2")) self.horizontalLayout_2.addWidget(self.label_2) self.textEdit = QtGui.QTextEdit(self.layoutWidget) self.textEdit.setObjectName(_fromUtf8("textEdit")) self.horizontalLayout_2.addWidget(self.textEdit) self.gridLayout.addLayout(self.horizontalLayout_2, 1, 0, 1, 1) self.horizontalLayout_3 = QtGui.QHBoxLayout() self.horizontalLayout_3.setObjectName(_fromUtf8("horizontalLayout_3")) self.label_3 = QtGui.QLabel(self.layoutWidget) self.label_3.setObjectName(_fromUtf8("label_3")) self.horizontalLayout_3.addWidget(self.label_3) self.lineEdit_2 = QtGui.QLineEdit(self.layoutWidget) self.lineEdit_2.setObjectName(_fromUtf8("lineEdit_2")) self.horizontalLayout_3.addWidget(self.lineEdit_2) self.gridLayout.addLayout(self.horizontalLayout_3, 2, 0, 1, 1) self.gridLayout_2.addLayout(self.gridLayout, 0, 0, 1, 1) self.pushButton = QtGui.QPushButton(self.layoutWidget) self.pushButton.setObjectName(_fromUtf8("pushButton")) self.gridLayout_2.addWidget(self.pushButton, 1, 0, 1, 1) MainWindow.setCentralWidget(self.centralwidget) self.statusbar = QtGui.QStatusBar(MainWindow) self.statusbar.setObjectName(_fromUtf8("statusbar")) MainWindow.setStatusBar(self.statusbar) self.retranslateUi(MainWindow) from textvalues import Valued QtCore.QObject.connect(self.pushButton, QtCore.SIGNAL(_fromUtf8("clicked()")), Valued.callingdata) QtCore.QMetaObject.connectSlotsByName(MainWindow) def retranslateUi(self, MainWindow): MainWindow.setWindowTitle(QtGui.QApplication.translate("MainWindow", "MainWindow", None, QtGui.QApplication.UnicodeUTF8)) self.label.setText(QtGui.QApplication.translate("MainWindow", "Title", None, QtGui.QApplication.UnicodeUTF8)) self.label_2.setText(QtGui.QApplication.translate("MainWindow", "Body", None, QtGui.QApplication.UnicodeUTF8)) self.label_3.setText(QtGui.QApplication.translate("MainWindow", "Tag", None, QtGui.QApplication.UnicodeUTF8)) self.pushButton.setText(QtGui.QApplication.translate("MainWindow", "Create", None, QtGui.QApplication.UnicodeUTF8)) if __name__ == "__main__": import sys app = QtGui.QApplication(sys.argv) MainWindow = QtGui.QMainWindow() ui = Ui_MainWindow() ui.setupUi(MainWindow) MainWindow.show() callingdata(ui) sys.exit(app.exec_())

子文件从子文件我试图获取输入文本值...

Child File From child file I'm trying to get the input text values...

from PyQt4 import QtCore, QtGui from blog_tool import Ui_MainWindow class Valued(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QWidget.__init__(self, parent) self.ui = Ui_MainWindow() app = self.ui.setupUi(self) def callingdata(): blog_title = app.lineEdit.text() blog_body = app.textEdit.toPlainText() blog_tag = app.lineEdit_2.text() print "Title\n\t%s\nBody\n\t%s\nTag\n\t%s" % (blog_title, blog_body, blog_tag)

推荐答案

您的示例代码有一些问题.主要问题是您正在修改由 pyuic 生成的 GUI 模块,您不应该尝试这样做.始终将 GUI 模块导入主应用程序,并在其中添加所有额外代码.

There are a few things wrong with your example code. The main problem is that you are modifying the GUI module generated by pyuic, which you should never be tempted to do. Always import the GUI module into your main application and add all the extra code there.

其他问题主要是由于未正确引用 GUI 中的小部件引起的.在您的示例中,这些都将成为您创建的 Ui_MainWindow 对象的属性,因此您可以通过 self.ui 访问它们.

The other problems are mainly caused by not referencing the widgets from the GUI properly. In your example, these will all become attributes of the Ui_MainWindow object you created, so you can acces them via self.ui.

我在下面重新编写了您的非 GUI 模块,以向您展示应该如何组合.但在尝试之前,请确保重新生成 GUI 模块.

I have re-written your non-GUI module below to show you how things should go together. But before you try it, make sure you regenerate your GUI module.

from PyQt4 import QtCore, QtGui from blog_tool import Ui_MainWindow class Valued(QtGui.QMainWindow): def __init__(self, parent=None): QtGui.QMainWindow.__init__(self, parent) self.ui = Ui_MainWindow() self.ui.setupUi(self) self.ui.pushButton.clicked.connect(self.callingdata) def callingdata(self): blog_title = self.ui.lineEdit.text() blog_body = self.ui.textEdit.toPlainText() blog_tag = self.ui.lineEdit_2.text() print "Title\n\t%s\nBody\n\t%s\nTag\n\t%s" % ( blog_title, blog_body, blog_tag) if __name__ == '__main__': import sys app = QtGui.QApplication(sys.argv) window = Valued() window.show() sys.exit(app.exec_())

更多推荐

从 PyQt GUI 类外部访问 GUI 元素 text()

本文发布于:2023-11-24 04:32:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1623981.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   PyQt   GUI   text

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!