我找到了一个关于如何在两个小部件之间进行淡入淡出的很好的例子。现在,我在这里尝试做的是将我在自己的类中构建的每个小部件分开。此阶段代码的问题在editor_类中,即行:
self.buttonBox.rejected.connect(stack.setPage1)
当我点击"取消"按钮,它给了我这些结果:
# Result: Traceback (most recent call last):
File "<string>", line 28, in paintEvent
AttributeError: 'FaderWidget' object has no attribute 'pixmap_opacity'
这是某种育儿错误吗?
谢谢
代码:
import sys
from PySide.QtCore import QTimeLine
from PySide.QtGui import *
class FaderWidget(QWidget):
def __init__(self, old_widget, new_widget):
QWidget.__init__(self, new_widget)
self.old_pixmap = QPixmap(new_widget.size())
old_widget.render(self.old_pixmap)
self.pixmap_opacity = 1.0
self.timeline = QTimeLine()
self.timeline.valueChanged.connect(self.animate)
self.timeline.finished.connect(self.close)
self.timeline.setDuration(333)
self.timeline.start()
self.resize(new_widget.size())
self.show()
def paintEvent(self, event):
painter = QPainter()
painter.begin(self)
painter.setOpacity(self.pixmap_opacity)
painter.drawPixmap(0, 0, self.old_pixmap)
painter.end()
def animate(self, value):
self.pixmap_opacity = 1.0 - value
self.repaint()
class StackedWidget(QStackedWidget):
def __init__(self, parent = None):
QStackedWidget.__init__(self, parent)
def setCurrentIndex(self, index):
self.fader_widget = FaderWidget(self.currentWidget(), self.widget(index))
QStackedWidget.setCurrentIndex(self, index)
def setPage1(self):
self.setCurrentIndex(0)
def setPage2(self):
self.setCurrentIndex(1)
def setPage3(self):
self.setCurrentIndex(2)
def setPage4(self):
self.setCurrentIndex(3)
class editor_(QWidget):
"""build a text editor"""
def __init__(self):
super(editor_, self).__init__()
editor = QTextEdit()
editor.setPlainText("Kawabungaaaa! "*100)
self.buttonBox = QDialogButtonBox(QDialogButtonBox.Ok | QDialogButtonBox.Cancel)
self.buttonBox.accepted.connect(self.close)
self.buttonBox.rejected.connect(stack.setPage1)
layout = QVBoxLayout()
layout.addWidget(editor)
layout.addWidget(self.buttonBox)
self.setLayout(layout)
class mainWidget(QWidget):
def __init__(self):
super(mainWidget, self).__init__()
self.setMinimumSize(500,300)
widget1 = QWidget()
widget2 = QCalendarWidget()
widget3 = QListView()
widget4 = editor_()
grid = QGridLayout()
allPlates = QPushButton("Calendar")
singlePlate = QPushButton("ListView")
allRoto = QPushButton("Editor")
allLighting = QPushButton("Buttons")
grid.addWidget(allPlates,0,0)
grid.addWidget(singlePlate,0,1)
grid.addWidget(allRoto,1,0)
grid.addWidget(allLighting,1,1)
widget1.setLayout(grid)
stack = StackedWidget()
stack.addWidget(widget1)
stack.addWidget(widget2)
stack.addWidget(widget3)
stack.addWidget(widget4)
layout = QHBoxLayout()
layout.addWidget(stack)
self.setLayout(layout)
allPlates.clicked.connect(stack.setPage2)
singlePlate.clicked.connect(stack.setPage3)
allRoto.clicked.connect(stack.setPage4)
allLighting.clicked.connect(stack.setPage1)
a = mainWidget()
a.show()
答案 0 :(得分:0)
您无法使用资源,但这不在当前环境中。 在您的情况下,堆栈未在editor_类中定义,因此它将生成错误。要解决这个问题,你必须在创建它们之后和它们共同的字段中连接,在你的情况下是mainWidget
# ...
stack.addWidget(widget4)
widget4.buttonBox.rejected.connect(stack.setPage1)
# ...