Python 从另一个文件PyQt4访问操作按钮

Python 从另一个文件PyQt4访问操作按钮,python,class,pyqt4,pyopengl,action-button,Python,Class,Pyqt4,Pyopengl,Action Button,我有一个.py文件,它是由Pyqt4的pyuic生成的。在这个文件中,我有一个工具栏和一个旋转图标,它连接到actionRotateaction。下面是代码的一个小分区 from PyQt4 import QtCore, QtGui try: _fromUtf8 = QtCore.QString.fromUtf8 except AttributeError: def _fromUtf8(s): return s try: _encoding = QtG

我有一个.py文件,它是由Pyqt4的pyuic生成的。在这个文件中,我有一个工具栏和一个旋转图标,它连接到
actionRotate
action。下面是代码的一个小分区

from PyQt4 import QtCore, QtGui

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    _encoding = QtGui.QApplication.UnicodeUTF8
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig, _encoding)
except AttributeError:
    def _translate(context, text, disambig):
        return QtGui.QApplication.translate(context, text, disambig)

class Ui_Program(object):
    def setupUi(self, UI_Class):
        UI_Class.setObjectName(_fromUtf8("UI_Class"))
               ... 
        self.toolBar = QtGui.QToolBar(UI_Class)
        self.toolBar.setObjectName(_fromUtf8("toolBar"))
        UI_Class.addToolBar(QtCore.Qt.TopToolBarArea, self.toolBar)
        self.toolBar.addAction(self.actionRotate)
        self.actionRotate = QtGui.QAction(UI_Class)
        self.actionRotate.setCheckable(True)
        self.actionRotate.setIcon(icon10)
        self.actionRotate.setObjectName(_fromUtf8("actionRotate"))
因此,如果我尝试访问
actionRotate
按钮,无论是否从另一个类选中它,它的工作方式与下面的示例类似

import PyQt4
import sys
from PyQt4.QtGui import *
from PyQt4 import QtGui, QtCore
from PyQt4 import QtGui
from DropDownActions import *
import pickle
import OpenGLcode
from OpenGL.GL import *
import PYQT_PROGRAM
import numpy as np
import sqlite3 as sq

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s

try:
    from OpenGL import GL
except ImportError:
    app = QtGui.QApplication(sys.argv)
    QtGui.QMessageBox.critical(None, "OpenGL hellogl",
            "PyOpenGL must be installed to run this example.")
    sys.exit(1)

class UI_main_subclass(QMainWindow):

    def __init__(self, ui_layout):

        QMainWindow.__init__(self)
        self.ui = ui_layout
        ui_layout.setupUi(self)
        ....
        var1 = ui_layout.actionRotate.isChecked()
但是,当我试图通过OpenGL代码实现操作时,我无法实现这一点。下面的代码显示了相关部分

from OpenGL.GL import *
from PyQt4.QtOpenGL import *
from PyQt4 import QtCore

class glWidget(QGLWidget, QMainWindow):
    resized = QtCore.pyqtSignal()

    xRotationChanged = QtCore.pyqtSignal(int)
    yRotationChanged = QtCore.pyqtSignal(int)
    zRotationChanged = QtCore.pyqtSignal(int)

    def __init__(self, ui_layout, parent = None):
        super(glWidget,self).__init__(parent)

    def mousePressEvent(self, event):
        if self.ui.actionRotate.isChecked(self): # this code gives error below
            print("test 1")
            x, y = event.x(), event.y()
            w, h = self.width(), self.height()
            # required to call this to force PyQt to read from the correct, updated buffer
            glReadBuffer(GL_FRONT)
            data = self.grabFrameBuffer()  # builtin function that calls glReadPixels internally
            rgba = QColor(data.pixel(x, y)).getRgb()  # gets the appropriate pixel data as an RGBA tuple
            message = "You selected pixel ({0}, {1}) with an RGBA value of {2}.".format(x, y, rgba)

            self.lastPos = event.pos()

        else:
            pass
误差为

AttributeError: 'glWidget' object has no attribute 'ui'
我不知道为什么这不允许我从pyuic访问main.py文件中生成的操作按钮


非常感谢您的帮助……

看来您已经准备好了大部分内容:您只需要正确地使用它们

在主窗口类中,将
ui\u布局传递给
glWidget

class SABRE2_main_subclass(QMainWindow):    
    def __init__(self, ui_layout):
        QMainWindow.__init__(self)
        self.ui = ui_layout
        ...
        self.OpenGLwidget = OpenGLcode.glWidget(ui_layout)
class glWidget(QGLWidget, QMainWindow):    
    def __init__(self, ui_layout, parent = None):
        super(glWidget,self).__init__(parent)
        self.ui = ui_layout
然后在
glWidget
中保留对它的引用:

class SABRE2_main_subclass(QMainWindow):    
    def __init__(self, ui_layout):
        QMainWindow.__init__(self)
        self.ui = ui_layout
        ...
        self.OpenGLwidget = OpenGLcode.glWidget(ui_layout)
class glWidget(QGLWidget, QMainWindow):    
    def __init__(self, ui_layout, parent = None):
        super(glWidget,self).__init__(parent)
        self.ui = ui_layout
现在,
glWidget
可以访问
ui\u布局的属性

    def mousePressEvent(self, event):
        if self.ui.actionRotate.isChecked():
            print("test 1")

包含
glWidget
类的模块甚至没有导入
Ui\u程序
Ui\u main\u子类
,更不用说尝试以任何方式使用它们了。所以我不明白你为什么认为它应该工作。我实际上是用
import UI_Program
导入的,我不能导入
UI_main_subclass
,因为
glWidget
类文件在
UI_main_subclass
中很重要。但是
glWidget
不会继承
UI_Program
,它也不会创建它的实例。那么为什么你认为它应该有一个
ui
属性呢?我怎样才能强制
glWidget
也从
ui\u程序
继承呢?你可以从这些github页面归档我的文件;谢谢,这解决了操作按钮的问题,但是它抛出了
下拉操作.statusMessage(self.ui,message)
glWidget
第86行中的箭头。我如何解决这个问题?您必须在
glWidget
中的所有位置使用
self.ui
,而不是
self.ui\u布局
(请参阅我答案中的修订代码)。然后您应该能够执行
下拉操作。statusMessage(self,message)
(因为
self
现在将具有
ui
属性)。(注:如果你觉得这个答案有用,请投票/接受)。