在类之外访问PyQT小部件 [英] Accessing a PyQT widget outside it's class

查看:59
本文介绍了在类之外访问PyQT小部件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

一个简单的情况证明很棘手.

a simple situation is proving fairly tricky.

我的目标是要获得一个窗口来更新另一个窗口中小部件的状态.解释:

My target is to get a window to update the status of a widget in another window. To explain:

我有一个带有一些数据"的窗口A,我希望将其写入窗口B的textEdit小部件中.主窗口和这些小部件的详细信息是

I have window A with some "data" and I want it written into the textEdit widget of window B. The specifics of the main window and the widgets are

class MainWin(object):
    def setupUi(self, MainWindow):
        MainWindow.setObjectName(_fromUtf8("MainWindow"))
        MainWindow.resize(1127, 880)

        self.centralwidget = QtGui.QWidget(MainWindow)
        self.centralwidget.setObjectName(_fromUtf8("centralwidget"))
        self.textEdit = QtGui.QTextEdit(self.centralwidget)
        self.textEdit.setGeometry(QtCore.QRect(20, 150, 1081, 661))
        self.textEdit.setObjectName(_fromUtf8("textEdit"))
        self.textEdit.isUndoRedoEnabled ()

我目前无法获得的是一个简单的方法,该方法从另一个窗口(和另一个文件)调用可能会更改textEdit中的内容.不确定我在做什么错,但是当我尝试从类中导入类或方法时,

What I am currently failing at obtaining is a simple method which called from another window (and another file) could change the content inside the textEdit. Not sure what I am doing wrong, but when I do try to import the class or a method from the class I always seem to have issue with the

TypeError:未绑定方法updEdit()必须使用MainWin实例作为第一个参数(而不是什么也不做)

TypeError: unbound method updEdit() must be called with MainWin instance as first argument (got nothing instead)

我非常愿意提出建议,在此先感谢!

I am very much open to suggestions, and thanks in advance!

编辑#1

来自文件Rep.py(我只是在调用第二个窗口)

from file Rep.py (the second window I am simply calling)

def addTemplate(self):
        data = self.textBrowser.toPlainText()
        MainWin.MainWin.addText(MainWindow.MainWin,data)
        self.close()

在第一个窗口中,我需要做的是:

From the first window, all I would need to do is:

def addText(self,data):
    self.textEdit.setText(data)

我对OOP还是陌生的,所以如果有一些非常愚蠢的错误,请原谅我.

I am fairly new to OOP so please forgive me if there's some rather stupid mistake.

推荐答案

创建第二个窗口时,请传递对第一个窗口的引用,并将其存储为属性:

When you create the second window, pass in a reference to the first window and store it as an attribute:

class SecondWindow(QMainWindow):
    def __init__(self, firstwindow, parent=None):
        super(SecondWindow, self).__init__(parent)
        self.firstwindow = firstwindow
        ...            

    def addTemplate(self):
        data = self.textBrowser.toPlainText()
        self.firstwindow.addText(data)


如果第二个窗口是 QMainWindow QDialog ,则还可以将第一个窗口作为第二个窗口的父级-在这种情况下,将不需要将其存储为属性,您可以执行以下操作:


If the second window is a QMainWindow or QDialog, you could also make the first window the parent of the second window - in which case, there would be no need to store it as an attribute and you could just do:

        self.parent().addText(data)

以这种方式进行操作还意味着第二个窗口将在第一个窗口关闭时自动关闭.

Doing it this way also means the second window will automatically close when the first window closes.

这篇关于在类之外访问PyQT小部件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆