将值从fileA传递到fileB并在qyqt5上更新它的ui?

时间:2018-10-24 08:33:44

标签: python pyqt pyqt5 qt-designer eric-ide

我有以下文件:

project folder:
__init__.py 
main.py
---ui subfolder:
   __init__.py 
   mainwindow.ui, 
   mainwindow.py
   optionsdialog.ui 
   optionsdialog.py

从qt设计器生成的mainwindow和optiondialog文件,我仅向py文件添加代码。

mainwindow.py:

from PyQt5 import QtWidgets
from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSlot, QTime, Qt 
from PyQt5.QtWidgets import (QMainWindow, QSystemTrayIcon, qApp, 
QStyle, QMenu,  QAction,  QApplication)
from .Ui_mainwindow import Ui_MainWindow

import sys
from .optionsdialog import OptionsDialog
from actions import Actions


    class MainWindow(QMainWindow, Ui_MainWindow):
    """
    Class documentation goes here.
    """
    def __init__(self, parent=None):
    """
        Constructor

        @param parent reference to the parent widget
        @type QWidget
        """
        super(MainWindow, self).__init__(parent)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.WindowCloseButtonHint)

        ..................
        ..................

def run():
    app = QtWidgets.QApplication(sys.argv)
    app.setQuitOnLastWindowClosed(False)
    ui = MainWindow()
    ui.show()
    return app.exec_()

optionsdialog.py:

from PyQt5 import QtCore
from PyQt5.QtCore import pyqtSlot
from PyQt5.QtWidgets import QDialog
from .Ui_optionsdialog import Ui_optDialog
from .mainwindow import MainWindow

class OptionsDialog(QDialog, Ui_optDialog):
    """
    Class documentation goes here.
    """   
    def __init__(self, parent_wnd):
    """
        Constructor

        @param parent reference to the parent widget
        @type QWidget
        """
        super(OptionsDialog, self).__init__(parent_wnd)
        self.setupUi(self)
        self.setWindowFlags(QtCore.Qt.Popup)
        self.ui=MainWindow()

    @pyqtSlot()
    def on_logoutRbox_clicked(self):
    """
    Slot documentation goes here.
    """
        logsel=self.logoutRbox.text()
        self.ui.actionLbl.setText(logsel)
        print(logsel)

    @pyqtSlot()
    def on_rebRbox_clicked(self):
    """
    Slot documentation goes here.
    """
        rebsel=self.rebRbox.text()
        print(rebsel)
   ...
   ...

我想将optionsdialog logoutRbox(也为rebRbox等)的值传递给mainwindow的标签(名称为actionLbl),并更新ui以显示新文本。 我试图将mainwindow的类导入optionsdialog,以便可以访问actionLbl并设置新的文本值(logsel),但是在此导入过程中总是会出错。我尝试了以下方法:

从.mainwindow导入MainWindow

未处理的ImportError “无法导入名称'MainWindow'”

从mainwindow导入MainWindow

未处理的ModuleNotFoundError “没有名为'mainwindow'的模块”

从ui.mainwindow导入MainWindow

未处理的ImportError “无法导入名称'MainWindow'”

所以,我被困在这里。例如,当我按下rebRbox按钮时,如何更新mainwindow的actionLbl文本?

-解决方案在这里找到: https://gis.stackexchange.com/questions/241380/passing-string-from-one-python-class-to-a-qtextedit-field-of-another-class-qgis

self.parent=parent
self.parent.actionLbl(logsel)

0 个答案:

没有答案