我是PyQt4的新手,正在尝试构建用于创建电路的GUI。我使用SchemDraw创建电路图,然后将其绘制到图形画布上,该图形画布是GUI的一部分。很好我还创建了一个向电路添加组件的按钮。这也起作用。不幸的是,容纳电路的ScrollArea没有更新大小/滚动条,因此电路变得比滚动区域大,因此无法正确显示。
我添加了两个屏幕快照以显示行为:
初始情况
添加一些组件后的情况
如果电路变宽,我想要的是能够向左滚动。 有人有主意吗?
我的代码:
import sys
from PyQt4 import QtGui
from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.backends.backend_qt4agg import FigureManagerQT as Fig
from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar
from matplotlib.figure import Figure
import PyQt4.QtCore as QtCore
import SchemDraw as schem
import SchemDraw.elements as e
class Window(QtGui.QApplication):
def __init__(self, sys_argv):
super(Window, self).__init__(sys_argv)
screen_resolution = self.desktop().screenGeometry()
width, height = screen_resolution.width(), screen_resolution.height()
self.main_window = QtGui.QMainWindow()
self.main_window.setWindowTitle('App')
self.main_window.showMaximized()
# set the layout
containerMain = QtGui.QWidget()
containerMain.setStyleSheet("background-color:lightGrey;")
mainLayout = QtGui.QVBoxLayout(containerMain)
containerGraph = QtGui.QWidget()
containerGraph.setStyleSheet("background-color:lightGrey ;")
graphLayout = QtGui.QVBoxLayout(containerGraph)
containerCircuit = QtGui.QWidget()
containerCircuit.setFixedWidth(width * 0.7)
containerCircuit.setFixedHeight(height * 0.45)
circuitLayout = QtGui.QVBoxLayout(containerCircuit)
containerInput = QtGui.QWidget()
containerInput.setStyleSheet("background-color:lightGrey ;")
inputLayout = QtGui.QVBoxLayout(containerInput)
containerUpperLayout = QtGui.QWidget()
containerUpperLayout.setStyleSheet("background-color:lightGrey;")
upperLayout = QtGui.QHBoxLayout(containerUpperLayout)
containerLowerLayout = QtGui.QWidget()
containerLowerLayout.setStyleSheet("background-color:lightGrey ;")
lowerLayout = QtGui.QHBoxLayout(containerLowerLayout)
#----------------------------Graph-Layout----------------------------#
self.figure = Figure()
self.canvas = FigureCanvas(self.figure)
graphLayout.addWidget(self.canvas)
#----------------------------Circuit-Layout----------------------------#
self.circuitDrawing = schem.Drawing()
self.circuitDrawing.push()
self.circuitFigure = Figure()
self.circuitCanvas = FigureCanvas(self.circuitFigure)
self.drawCircuit()
self.scrollArea = QtGui.QScrollArea()
self.scrollArea.setWidget(self.circuitCanvas)
buttonDummy1 = QtGui.QPushButton('Add Element to Circuit')
buttonDummy2 = QtGui.QPushButton('Button')
buttonDummy1.clicked.connect(self.drawCircuit)
circuitLayout.addWidget(self.scrollArea)
circuitLayout.addWidget(buttonDummy1)
circuitLayout.addWidget(buttonDummy2)
#----------------------------Input-Layout----------------------------#
buttonDummy3 = QtGui.QPushButton('Button')
buttonDummy3.setFixedSize(50,50)
buttonDummy4 = QtGui.QPushButton('Button')
buttonDummy4.setFixedSize(50,50)
inputLayout.addWidget(buttonDummy3)
inputLayout.addWidget(buttonDummy4)
inputLayout.setContentsMargins(50,50,50,50)
#----------------------------Main-Layout----------------------------#
upperLayout.addWidget(containerCircuit)
upperLayout.addWidget(containerInput)
lowerLayout.addWidget(containerGraph)
mainLayout.addWidget(containerUpperLayout)
mainLayout.addWidget(containerLowerLayout)
self.main_window.setCentralWidget(containerMain)
def drawCircuit(self):
self.circuitDrawing.add(e.RBOX)
ax = self.circuitFigure.add_subplot(111)
# discards the old graph
ax.clear()
# plot data
self.circuitDrawing.draw(ax = ax)
# refresh canvas
self.circuitCanvas.draw()
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
main = Window(sys.argv)
sys.exit(app.exec_())
更新信息: 如果我添加self.scrollArea.setWidgetResizable(True)行,则图形将接管孔区域,但水平滚动的滚动条也不会显示。
如果我将固定大小设置为self.circuitCanvas,则会得到一个水平滚动条,但是如果电路越来越大,则不会更新。