首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从另一个文件PyQt4访问操作按钮

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

Stack Overflow用户
提问于 2017-09-27 04:09:27
回答 1查看 646关注 0票数 1

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

代码语言:javascript
复制
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按钮,不管它是否是从另一个类中选中的,它的工作方式就像下面的示例一样;

代码语言:javascript
复制
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代码到达操作时,我无法做到这一点。下面的代码显示了相关部分;

代码语言:javascript
复制
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

错误是;

代码语言:javascript
复制
AttributeError: 'glWidget' object has no attribute 'ui'

我不知道为什么这不允许我到达从pyuic生成的主.py文件中生成的操作按钮?

任何帮助都很感激..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-09-27 19:42:39

似乎你已经准备好了大部分的东西:你只需要正确地利用它们。

在主窗口类中,将ui_layout传递给glWidget

代码语言:javascript
复制
class SABRE2_main_subclass(QMainWindow):    
    def __init__(self, ui_layout):
        QMainWindow.__init__(self)
        self.ui = ui_layout
        ...
        self.OpenGLwidget = OpenGLcode.glWidget(ui_layout)

然后在glWidget中保存对它的引用

代码语言:javascript
复制
class glWidget(QGLWidget, QMainWindow):    
    def __init__(self, ui_layout, parent = None):
        super(glWidget,self).__init__(parent)
        self.ui = ui_layout

现在glWidget可以访问ui_layout的属性了。

代码语言:javascript
复制
    def mousePressEvent(self, event):
        if self.ui.actionRotate.isChecked():
            print("test 1")
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46439336

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档