Пишу приложение. должно рисовать график по данным из файла. paintevent срабатывает сразу при открытии программы. я сделал функцию, открытия файла. она вызывается раньше. График рисуется, если открыть файл снова, то ничего не изменяется. Код | #!/usr/bin/python2 from PyQt4.QtGui import QMessageBox
import sys, random, math from PyQt4 import QtGui, QtCore from xml.dom.minidom import *
class MainWindow(QtGui.QMainWindow):
def __init__(self): QtGui.QMainWindow.__init__(self)
self.resize(650, 350) self.setWindowTitle('Graphics-DB')
#----------------------------Menu------------------------------------# open_mn = QtGui.QAction(QtGui.QIcon('icons/open.png'), 'Open', self) open_mn.setShortcut('Ctrl+O') open_mn.setStatusTip('Open XML file') self.connect(open_mn, QtCore.SIGNAL('triggered()'), self.showDialog)
exit = QtGui.QAction(QtGui.QIcon('icons/exit.png'), 'Exit', self) exit.setShortcut('Ctrl+Q') exit.setStatusTip('Exit application') self.connect(exit, QtCore.SIGNAL('triggered()'), QtCore.SLOT('close()'))
menubar = self.menuBar() file = menubar.addMenu('&File') file.addAction(open_mn) file.addAction(exit)
self.data = self.showDialog() self.update() #--------------------------------Prosmotr texta --------------------------#
def showDialog(self): filenames = QtGui.QFileDialog.getOpenFileName(self, 'Open file', '/home/evgeniy') files = open(filenames) data = files.read() self.update() return data
def paintEvent(self, QPaintEvent): xml = parseString(self.data) mydom = xml.getElementsByTagName('part') time = [] rate = [] for i in mydom: time.append(eval(i.getAttribute('time'))) rate.append(eval(i.getAttribute('rate')))
paint = QtGui.QPainter() paint.begin(self)
pen = QtGui.QPen(QtCore.Qt.green, 2, QtCore.Qt.SolidLine) paint.setPen(pen)
size = self.size() for i in range(9): x = (size.width() / 2) + time[i] y = (size.height() / 2) - rate[i] x1 = (size.width() / 2) + time[i+1] y1 = (size.height() / 2) - rate[i+1] paint.drawLine(x, y, x1, y1) paint.end()
app = QtGui.QApplication(sys.argv) main = MainWindow() main.show() sys.exit(app.exec_())
|
|