简体   繁体   English

从另一个文件访问 PyQt Gui 元素

[英]Accesing PyQt Gui elements from another file

I am learning PyQt and coming from webdesign, so excuse this question that must have very obvious answer.So I am building a PyQt application and I would like to spread methods to several files to correspond different parts of GUI.我正在学习 PyQt 并来自网页设计,所以请原谅这个必须有非常明显答案的问题。所以我正在构建一个 PyQt 应用程序,我想将方法传播到多个文件以对应 GUI 的不同部分。 How can I access textbox locating in fileA.py from fileB.py.如何从 fileB.py 访问位于 fileA.py 中的文本框。 :

#fileA.py
import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
import fileB

class MyApp(QtGui.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtGui.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)


if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        window = MyApp()
        window.show()

        #This works all fine
        def pressed():
            window.plainTextEdit.appendPlainText("Hello")


        window.pushButton.pressed.connect(pressed)


        window.button2.pressed.connect(fileB.func3)
        sys.exit(app.exec_())    

Now, in this file I would like to use textbox from fileA.py现在,在这个文件中,我想使用 fileA.py 中的文本框

#fileB.py
import fileA

    #How do I access window.plainTextEdit from fileA.py
def func3():
    print "hello"
    fileA.window.plainTextEdit.appendPlainText("Hello")

What am I doing wrong?我究竟做错了什么? What would be best way to spread functionality to multiple files if not this?如果不是这样,将功能传播到多个文件的最佳方法是什么?

Thank you for taking time to read this.感谢您花时间阅读本文。

You can take advantage of Python's class inheritance, like so:您可以利用 Python 的 class inheritance,如下所示:

fileA.py :文件A.py

import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow
import fileB

class MyApp(fileB.MyApp, QtGui.QMainWindow):
  def __init__(self):
     self.MyMethod()
     # Should print 'foo'

fileB.py :文件B.py

import sys
from PyQt4 import QtGui, QtCore
from gui1 import Ui_MainWindow

class MyApp(QtGui.QMainWindow):
  def MyMethod(self):
    print 'foo'

Well, first off, the code under if __name__ == "__main__" will never be run when you are importing fileA.py , and so fileA.window does not exist.好吧,首先,当您导入fileA.py时, if __name__ == "__main__"下的代码将永远不会运行,因此fileA.window不存在。 That's what it should do: run only when __name__ is "__main__" , ie run as a top-level program.这就是它应该做的:仅在__name__"__main__"时运行,即作为顶级程序运行。 Instead, you should import fileA.py , create the QApplication and window again, then access window.plainTextEdit .相反,您应该导入fileA.py ,再次创建QApplicationwindow ,然后访问window.plainTextEdit However, this creates a very tight coupling between the code, as you are directly accessing a widget in MyApp from fileB .但是,这会在代码之间产生非常紧密的耦合,因为您直接从fileB访问MyApp中的小部件。 It might be better if instead, you expose a method in MyApp that appends to the text box instead of having fileB.py do it directly.相反,如果您在MyApp中公开一个附加到文本框的方法,而不是让fileB.py直接执行它,这可能会更好。 So you may want to think about what you want to do and how to structure your program.因此,您可能需要考虑您想要做什么以及如何构建您的程序。

Of course, you don't have to structure your code that way;当然,您不必以这种方式构建代码。 you could simply do你可以简单地做

window = MyApp()
window.plainTextEdit.appendPlainText("Hello")

in fileB if you wanted.如果您愿意,可以在fileB中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM