窗口没有正确显示 qt python
Posted
技术标签:
【中文标题】窗口没有正确显示 qt python【英文标题】:window doesnt appear correctly qtpython 【发布时间】:2019-03-01 15:03:40 【问题描述】:单击按钮时,我正在尝试打开一个简单的窗口。我复制的这个简单窗口来自:MultipleplotAxis.py
我的代码是:
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(264, 248)
MainWindow.setMinimumSize(QtCore.QSize(264, 248))
MainWindow.setMaximumSize(QtCore.QSize(264, 248))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 264, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
self.pushButton.clicked['bool'].connect(self.lanzar_grafica_3_ejes)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
def lanzar_grafica_3_ejes(self):
self.lanzar_a_canasta = lanzar()
self.lanzar_a_canasta.setupUi(self)
class lanzar(object):
###########
# COPY FROM PYQTGRAPH.ORG
###########
def __init__(self):
print('__init__ lanzar class')
def setupUi(self, MainWindow):
import time
pg.mkQApp()
pw = pg.PlotWidget()
pw.show()
time.sleep(5)
pw.setWindowTitle('pyqtgraph example: MultiplePlotAxes')
p1 = pw.plotItem
p1.setLabels(left='axis 1')
## create a new ViewBox, link the right axis to its coordinate system
p2 = pg.ViewBox()
p1.showAxis('right')
p1.scene().addItem(p2)
p1.getAxis('right').linkToView(p2)
p2.setXLink(p1)
p1.getAxis('right').setLabel('axis2', color='#0000ff')
## create third ViewBox.
## this time we need to create a new axis as well.
p3 = pg.ViewBox()
ax3 = pg.AxisItem('right')
p1.layout.addItem(ax3, 2, 3)
p1.scene().addItem(p3)
ax3.linkToView(p3)
p3.setXLink(p1)
ax3.setZValue(-10000)
ax3.setLabel('axis 3', color='#ff0000')
## Handle view resizing
def updateViews():
## view has resized; update auxiliary views to match
global p1, p2, p3
p2.setGeometry(p1.vb.sceneBoundingRect())
p3.setGeometry(p1.vb.sceneBoundingRect())
## need to re-update linked axes since this was called
## incorrectly while views had different shapes.
## (probably this should be handled in ViewBox.resizeEvent)
p2.linkedViewChanged(p1.vb, p2.XAxis)
p3.linkedViewChanged(p1.vb, p3.XAxis)
updateViews()
p1.vb.sigResized.connect(updateViews)
p1.plot([10,20,40,80,40,20],[1,2,4,8,16,32])
p2.addItem(pg.PlotCurveItem([10,20,40,80,40,20],[12,13,14,15,16,17], pen='b'))
p3.addItem(pg.PlotCurveItem([10,20,40,80,40,20],[32,16,8,4,2,1], pen='r'))
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
MainWindow = QtWidgets.QMainWindow()
ui = Ui_MainWindow()
ui.setupUi(MainWindow)
MainWindow.show()
sys.exit(app.exec_())
当我按下按钮时,我想出现的窗口并没有完全出现,我的意思是:
这是我想要出现的窗口: ,但是当我点击按钮时,只会出现里面什么都没有的框架。我还编写了一个计时器代码,因为如果没有,窗口会立即消失。
我想我以错误的方式调用课程。
有什么帮助吗?
【问题讨论】:
您只需要将setupUi
中的pw
替换为self.pw
并在该函数的开头添加global p1, p2, p3
行。 (那么你也可以删除time.sleep(5)
)
【参考方案1】:
试试看:
from PyQt5 import QtCore, QtGui, QtWidgets
import pyqtgraph as pg
from pyqtgraph.Qt import QtCore, QtGui
import numpy as np
#import time
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
MainWindow.setObjectName("MainWindow")
MainWindow.resize(264, 248)
MainWindow.setMinimumSize(QtCore.QSize(264, 248))
# MainWindow.setMaximumSize(QtCore.QSize(264, 248))
self.centralwidget = QtWidgets.QWidget(MainWindow)
self.centralwidget.setObjectName("centralwidget")
self.gridLayout = QtWidgets.QGridLayout(self.centralwidget)
self.gridLayout.setObjectName("gridLayout")
self.pushButton = QtWidgets.QPushButton(self.centralwidget)
self.pushButton.setObjectName("pushButton")
self.gridLayout.addWidget(self.pushButton, 0, 0, 1, 1)
MainWindow.setCentralWidget(self.centralwidget)
self.menubar = QtWidgets.QMenuBar(MainWindow)
self.menubar.setGeometry(QtCore.QRect(0, 0, 264, 21))
self.menubar.setObjectName("menubar")
MainWindow.setMenuBar(self.menubar)
self.statusbar = QtWidgets.QStatusBar(MainWindow)
self.statusbar.setObjectName("statusbar")
MainWindow.setStatusBar(self.statusbar)
self.retranslateUi(MainWindow)
# self.pushButton.clicked['bool'].connect(self.lanzar_grafica_3_ejes)
QtCore.QMetaObject.connectSlotsByName(MainWindow)
def retranslateUi(self, MainWindow):
_translate = QtCore.QCoreApplication.translate
MainWindow.setWindowTitle(_translate("MainWindow", "MainWindow"))
self.pushButton.setText(_translate("MainWindow", "PushButton"))
# def lanzar_grafica_3_ejes(self): ##(MainWindow): #
# self.lanzar_a_canasta = Lanzar()
# self.lanzar_a_canasta.setupUi() #(self)
class Lanzar(QtWidgets.QWidget): #(object):
###########
# COPY FROM PYQTGRAPH.ORG
###########
def __init__(self):
super().__init__()
print('__init__ Lanzar class')
def setupUi(self, MainWindow):
pg.mkQApp()
self.pw = pg.PlotWidget()
# self.pw.show()
# time.sleep(5)
self.pw.setWindowTitle('pyqtgraph example: MultiplePlotAxes')
self.p1 = self.pw.plotItem
self.p1.setLabels(left='axis 1')
## create a new ViewBox, link the right axis to its coordinate system
self.p2 = pg.ViewBox()
self.p1.showAxis('right')
self.p1.scene().addItem(self.p2)
self.p1.getAxis('right').linkToView(self.p2)
self.p2.setXLink(self.p1)
self.p1.getAxis('right').setLabel('axis2', color='#0000ff')
## create third ViewBox.
## this time we need to create a new axis as well.
self.p3 = pg.ViewBox()
ax3 = pg.AxisItem('right')
self.p1.layout.addItem(ax3, 2, 3)
self.p1.scene().addItem(self.p3)
ax3.linkToView(self.p3)
self.p3.setXLink(self.p1)
ax3.setZValue(-10000)
ax3.setLabel('axis 3', color='#ff0000')
## Handle view resizing
def updateViews():
## view has resized; update auxiliary views to match
# global p1, p2, p3
self.p2.setGeometry(self.p1.vb.sceneBoundingRect())
self.p3.setGeometry(self.p1.vb.sceneBoundingRect())
## need to re-update linked axes since this was called
## incorrectly while views had different shapes.
## (probably this should be handled in ViewBox.resizeEvent)
self.p2.linkedViewChanged(self.p1.vb, self.p2.XAxis)
self.p3.linkedViewChanged(self.p1.vb, self.p3.XAxis)
updateViews()
self.p1.vb.sigResized.connect(updateViews)
self.p1.plot([10,20,40,80,40,20],[1,2,4,8,16,32])
self.p2.addItem(pg.PlotCurveItem([10,20,40,80,40,20],[12,13,14,15,16,17], pen='b'))
self.p3.addItem(pg.PlotCurveItem([10,20,40,80,40,20],[32,16,8,4,2,1], pen='r'))
class Window(QtWidgets.QMainWindow, Ui_MainWindow):
def __init__(self):
super().__init__()
self.setupUi(self)
self.pushButton.clicked.connect(self.lanzar_grafica_3_ejes)
def lanzar_grafica_3_ejes(self):
self.lanzar_a_canasta = Lanzar()
self.lanzar_a_canasta.setupUi(self.lanzar_a_canasta)
self.gridLayout.addWidget(self.lanzar_a_canasta.pw, 1, 0, 1, 5)
if __name__ == "__main__":
import sys
app = QtWidgets.QApplication(sys.argv)
w = Window()
w.show()
w.resize(630, 550)
sys.exit(app.exec_())
【讨论】:
以上是关于窗口没有正确显示 qt python的主要内容,如果未能解决你的问题,请参考以下文章
如何使用 Linux 和 python 在我的 Qt 窗口中显示 word/excel 文档?
为啥我执行 Python QT5 代码时没有显示图标和图像?
Python3 + Pillow + QT5:当我调整包含图像的标签大小时崩溃