这不是重复的!我不想将标题过长,因此它的简短版本可能看起来像是重复的。这个问题比标题中的要大。
环境:我正在尝试使用PyQt5和Qt-Designer构建绘画应用程序。我的项目中有三个文件: main.py , slider.py , ui.py (由Qt-Designer生成的UI pyuic)。
我需要什么::每次更改 Slider 值时,我都需要更改 App 的画笔大小(自变量)。 / p>
问题本身::App类从我无法编辑的文件(每次生成)中继承Ui_MainWindow类。 Ui_MainWindow将Slider Class设置为其属性。因此,基本上Slider是App的属性,在进入Slider类时,我需要更改App的变量“ canvas”。
我不知道如何在内部类方法中访问外部类变量。特别是当我无法编辑ui.py时,我可以调用Slider(self)而不是Slider(self.sidebar)。
main.py
import sys
from PyQt5 import uic
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from ui import Ui_MainWindow
class App(QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
if __name__ == '__main__':
application = QApplication(sys.argv)
example = App()
example.show()
sys.exit(application.exec_())
slider.py
from PyQt5 import uic
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.QtWidgets import *
from functools import partial
import inspect
class Slider(QSlider):
def __init__(self, parent=None):
super().__init__(parent)
self.valueChanged.connect(self.valueChange)
self.setMinimum(8)
self.setMaximum(64)
self.setTickInterval(8)
def valueChange(self):
pass
# Here I need to change App.canvas, have no idea how to do it
ui.py
# -*- coding: utf-8 -*-
# Form implementation generated from reading ui file 'main.ui'
#
# Created by: PyQt5 UI code generator 5.11.3
#
# WARNING! All changes made in this file will be lost!
from PyQt5 import QtCore, QtGui, QtWidgets
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(912, 715)
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.horizontalLayout = QtWidgets.QHBoxLayout(self.centralwidget)
self.horizontalLayout.setObjectName("horizontalLayout")
self.sidebar = QtWidgets.QWidget(self.centralwidget)
self.sidebar.setMinimumSize(QtCore.QSize(388, 0))
self.sidebar.setAutoFillBackground(True)
self.sidebar.setObjectName("sidebar")
self.clearButton = QtWidgets.QPushButton(self.sidebar)
self.clearButton.setGeometry(QtCore.QRect(20, 30, 91, 41))
self.clearButton.setObjectName("clearButton")
# This is where Slider is called
# The file is generated so I can't edit it
self.brushSizeSlider = Slider(self.sidebar)
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
# ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
self.brushSizeSlider.setGeometry(QtCore.QRect(20, 120, 160, 22))
self.brushSizeSlider.setOrientation(QtCore.Qt.Horizontal)
self.brushSizeSlider.setObjectName("brushSizeSlider")
self.brushSizeLabel = QtWidgets.QLabel(self.sidebar)
self.brushSizeLabel.setGeometry(QtCore.QRect(20, 90, 101, 16))
self.brushSizeLabel.setObjectName("brushSizeLabel")
self.brushColorDial = QtWidgets.QDial(self.sidebar)
self.brushColorDial.setGeometry(QtCore.QRect(20, 180, 50, 64))
self.brushColorDial.setObjectName("brushColorDial")
self.brushColorLabel = QtWidgets.QLabel(self.sidebar)
self.brushColorLabel.setGeometry(QtCore.QRect(20, 160, 101, 16))
self.brushColorLabel.setObjectName("brushColorLabel")
self.horizontalLayout.addWidget(self.sidebar)
self.canvas = Canvas(self.centralwidget)
self.canvas.setMinimumSize(QtCore.QSize(500, 500))
self.canvas.setMaximumSize(QtCore.QSize(5000, 5000))
self.canvas.setBaseSize(QtCore.QSize(500, 500))
self.canvas.setCursor(QtGui.QCursor(QtCore.Qt.ArrowCursor))
self.canvas.setAutoFillBackground(True)
self.canvas.setStyleSheet("canvas{background-color: rgb(70, 70, 50);}")
self.canvas.setObjectName("canvas")
self.horizontalLayout.addWidget(self.canvas)
MainWindow.setCentralWidget(self.centralwidget)
self.retranslateUi(MainWindow)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.clearButton.setText(_translate("MainWindow", "Clear canvas"))
self.brushSizeLabel.setText(_translate("MainWindow", "Current brush size:"))
self.brushColorLabel.setText(_translate("MainWindow", "Current brush color:"))
from canvas import Canvas
from slider import Slider