【发布时间】:2019-10-12 10:45:50
【问题描述】:
我有一个 python (2.7) 程序 (myGraph.py),它使用 matplotlib 通过函数 showGraph() 创建一个图形。
现在我从 QDialog 调用这个函数 showGraph() 来显示如下图:
import myGraph
# ...
# ....
class myTable(QDialog):
# ...
# ...
myGraph.showGraph(graphName)
与“graphName”相关的图表正确显示,但 QDialog (myTable) 几乎立即重新出现在图表上方,我无法仔细查看图表,例如展开和滚动它。为此,我必须关闭我不希望的 QDialog (myTable)。因为,我必须看到从 QDialog 中选择的不同 graphName 上传递的不同图形。
所以,基本上我希望从 QDialog 中选择一个“graphName”并调用我的 matplotlib 程序来查看图形,仔细检查它。在此期间,我的 QDialog 应该在图表后面。一旦我关闭图表,QDialog 应该会重新出现,然后我会从对话框中选择一个新的“graphName”并仔细检查它,等等。
程序框架如下:
=============1。分析器.py==================================
import sys
from PyQt5.QtWidgets import QMainWindow, QApplication, QMessageBox
from PyQt5 import uic
from PyQt5.QtCore import QCoreApplication
Ui_MainWindwo, QtBaseClass = uic.loadUiType('analyser.ui')
class MyApp(QMainWindow):
super(MyApp, self).__init__()
self.ui = Ui.MainWindow()
self.ui.setupUi(self)
self.ui.alertPushButton.clicked.connect(self.createAlerts)
:
: ## different other signal-slot connections etc.
:
def createAlerts(self):
:
: ##Building up of a list 'alrtLst'
: ##for example final alrtLst is:
alrtLst = [['Graph1', 'Sales1-3', 'ABM', '200'],
['Graph2', 'Sales4-8', 'KNT', '500'],
['Graph3', 'Sales9-10', 'MT', '400'],
['Graph4', 'Sales11-12', 'TSL', '500']]
from alertTable import AlertTable
alert_table = AlertTable(alrtLst)
alert_table.exec_()
if __name__ == '__main__':
app = QCoreApplcation.instance()
if app is None:
app = QApplication(sys.argv)
window = MyApp()
window.show()
sys.exit(app.exec_())
=============2。 alertTable.py=================================
from PyQt5.QtWidgets import QDialog, QTableWidgetItem
from PyQt5 import uic
import myGraph ##myGraph.py program
class AlertTable(QDialog):
def __init__(self, alrtLst):
super(AlertTable, self).__init__()
uic.loadUi('mytable.ui', self)
self.tableWidget.doubleClicked.connect(self.tableClick)
self.tableWidget.setColumnCount(4)
header_labels = ['Label1', 'Label2', 'Label3', 'Label4']
self.tableWidget.setHorizontalHeaderLabels(header_labels)
for row in alrtLst:
inx = alrtLst.index(row)
self.tableWidget.insertRow(inx)
self.tableWidget.setItem(inx,0,QTableWidgetItem(str(row[0])))
self.tableWidget.setItem(inx,1,QTableWidgetItem(str(row[1])))
self.tableWidget.setItem(inx,2,QTableWidgetItem(str(row[2])))
self.tableWidget.setItem(inx,3,QTableWidgetItem(str(row[3])))
def tableClick(self):
row = self.tableWidget.currentItem().row()
cell = self.tableWidget.item(row, 0),text()
myGraph.showGraph(cell)
=============3。 myGraph.py=================================
import matplotlib.pyplot as plt
def showGraph(grphName):
dfc = pd.read_csv('E:\\analyzedFiles\\'+grphName+'.csv')
:
: ##Construct graph
:
plt.show()
=============4。 Analyzer.ui==================================
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>shareAnlyserMainWindow</class>
<widget class="QMainWindow" name="anlyserMainWindow">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>484</width>
<height>278</height>
</rect>
</property>
<property name="windowTitle">
<string>Analyser</string>
</property>
<widget class="QWidget" name="centralwidget">
<layout class="QGridLayout" name="gridLayout_3">
<item row="6" column="0" colspan="3">
<layout class="QGridLayout" name="gridLayout_2">
<item row="0" column="0">
<widget class="QProgressBar" name="progressBar">
<property name="value">
<number>24</number>
</property>
</widget>
</item>
<item row="1" column="0">
<spacer name="verticalSpacer_3">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="0" column="0" colspan="5">
<widget class="QLabel" name="label">
<property name="font">
<font>
<pointsize>28</pointsize>
<weight>75</weight>
<bold>true</bold>
</font>
</property>
<property name="text">
<string>ANALYSER</string>
</property>
<property name="alignment">
<set>Qt::AlignCenter</set>
</property>
</widget>
</item>
<item row="1" column="0" colspan="5">
<widget class="Line" name="line">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</widget>
</item>
<item row="4" column="0">
<spacer name="verticalSpacer_5">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>15</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="3" rowspan="4">
<widget class="Line" name="line_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
</widget>
</item>
<item row="2" column="2">
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>0</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="4" rowspan="4">
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QPushButton" name="consolidatePushButton">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="toolTip">
<string>Consolidate all daily data</string>
</property>
<property name="text">
<string>Consolidate</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="analysePushButton">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="toolTip">
<string>Analyse each value movement</string>
</property>
<property name="text">
<string>Analyse</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="alertPushButton">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="toolTip">
<string>Create alerts for significant movement of specific company value</string>
</property>
<property name="text">
<string>Create Alert</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_4">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>58</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="exitPushButton">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="toolTip">
<string>Exit application</string>
</property>
<property name="text">
<string>E&xit</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer_6">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>28</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="label_5">
<property name="text">
<string><html><head/><body><p align="right">&#169;Bidyut</p></body></html></string>
</property>
<property name="textFormat">
<enum>Qt::AutoText</enum>
</property>
</widget>
</item>
</layout>
</item>
<item row="3" column="0" colspan="3">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_2">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Current Month</string>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QComboBox" name="monthComboBox">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<item>
<property name="text">
<string>Select Month ...</string>
</property>
</item>
<item>
<property name="text">
<string>January</string>
</property>
</item>
<item>
<property name="text">
<string>February</string>
</property>
</item>
<item>
<property name="text">
<string>March</string>
</property>
</item>
<item>
<property name="text">
<string>April</string>
</property>
</item>
<item>
<property name="text">
<string>May</string>
</property>
</item>
<item>
<property name="text">
<string>June</string>
</property>
</item>
<item>
<property name="text">
<string>July</string>
</property>
</item>
<item>
<property name="text">
<string>August</string>
</property>
</item>
<item>
<property name="text">
<string>September</string>
</property>
</item>
<item>
<property name="text">
<string>October</string>
</property>
</item>
<item>
<property name="text">
<string>November</string>
</property>
</item>
<item>
<property name="text">
<string>December</string>
</property>
</item>
</widget>
</item>
<item row="0" column="2" colspan="2">
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>128</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_3">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Current Year</string>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="yearLineEdit">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
</widget>
</item>
<item row="1" column="2" colspan="2">
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>128</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="2" column="1">
<spacer name="verticalSpacer_2">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>13</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="0" colspan="2">
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>228</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item row="3" column="2">
<widget class="QPushButton" name="enterPushButton">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Enter</string>
</property>
</widget>
</item>
<item row="3" column="3">
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>58</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item row="4" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLabel" name="label_4">
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Selected Year Block:</string>
</property>
</widget>
</item>
<item>
<widget class="QLabel" name="yearBlockLabel">
<property name="text">
<string>xxxx-xxxx,xxxx-xxxx,xxxx-xxxx</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<widget class="QMenuBar" name="menubar">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>484</width>
<height>21</height>
</rect>
</property>
<widget class="QMenu" name="menuFile">
<property name="title">
<string>File</string>
</property>
<addaction name="actionGetValues"/>
<addaction name="separator"/>
<addaction name="actionExit"/>
</widget>
<widget class="QMenu" name="menuProcess">
<property name="title">
<string>Process</string>
</property>
<addaction name="actionConsolidate"/>
<addaction name="actionAnalyse"/>
<addaction name="actionCreateAlert"/>
</widget>
<widget class="QMenu" name="menuHelp">
<property name="title">
<string>Help</string>
</property>
<addaction name="actionAbout"/>
<addaction name="actionTutorial"/>
</widget>
<widget class="QMenu" name="menuSettings">
<property name="title">
<string>Settings</string>
</property>
<addaction name="actionSetTI"/>
</widget>
<addaction name="menuFile"/>
<addaction name="menuProcess"/>
<addaction name="menuSettings"/>
<addaction name="menuHelp"/>
</widget>
<widget class="QStatusBar" name="statusbar"/>
<action name="actionExit">
<property name="text">
<string>E&xit</string>
</property>
<property name="shortcut">
<string>Ctrl+X</string>
</property>
</action>
<action name="actionConsolidate">
<property name="text">
<string>&Consolidate</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+C</string>
</property>
<property name="shortcutContext">
<enum>Qt::ApplicationShortcut</enum>
</property>
</action>
<action name="actionAnalyse">
<property name="text">
<string>&Analyse</string>
</property>
<property name="shortcut">
<string>Ctrl+Shift+A</string>
</property>
</action>
<action name="actionCreateAlert">
<property name="text">
<string>Create A&lert</string>
</property>
<property name="shortcut">
<string>Ctrl+L</string>
</property>
</action>
<action name="actionAbout">
<property name="text">
<string>A&bout</string>
</property>
<property name="shortcut">
<string>Ctrl+B</string>
</property>
</action>
<action name="actionTutorial">
<property name="text">
<string>&Tutorial</string>
</property>
<property name="shortcut">
<string>Ctrl+T</string>
</property>
</action>
<action name="actionGetValues">
<property name="text">
<string>GetValues</string>
</property>
</action>
<action name="actionSetTI">
<property name="text">
<string>Technical Indicators</string>
</property>
</action>
</widget>
<resources/>
<connections/>
</ui>
=============5。 myTable.ui=================================
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>trytable</class>
<widget class="QDialog" name="trytable">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>551</width>
<height>347</height>
</rect>
</property>
<property name="windowTitle">
<string>Try Table</string>
</property>
<widget class="QWidget" name="">
<property name="geometry">
<rect>
<x>20</x>
<y>50</y>
<width>501</width>
<height>291</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableWidget" name="tableWidget"/>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<property name="text">
<string>Show Table</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="closeButton">
<property name="text">
<string>Close</string>
</property>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>18</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</widget>
<resources/>
<connections/>
</ui>
【问题讨论】:
-
@S.Nick,按照建议修改了问题。谢谢
-
抱歉,我无法重现您的问题,因为您还没有发布
analyser.ui和mytable.ui -
@S.尼克,是的,我明白。我已经包含了 .ui 文件。但是,您已经给出了解决方案。请检查我在 约束 上给出的评论并帮助我。谢谢。
标签: python python-2.7 matplotlib pyqt pyqt5