[PyQt] cant import progress bar from another py file - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: GUI (https://python-forum.io/forum-10.html) +--- Thread: [PyQt] cant import progress bar from another py file (/thread-1248.html) |
cant import progress bar from another py file - swipis - Dec-17-2016 Hello First Im new in python and pyqt. I found progress bar py file on the internet and to use this file I should to import to main py file, so I did: add __init__.py file to src folder as well in root folder in main.py file I importing > from src.progressbar import prgBar in main.py file in __init__ function self.someprogressbar = prgBar() and set value for progress bar, but no luck so far. here is the main.py code: from PyQt4 import QtGui, QtCore import sys from src.testCprg import cPrg class mainWindow(QtGui.QWidget): def __init__(self): self.otherFile = cPrg(self) super(mainWindow, self).__init__() self.initUI() def initUI(self): self.label = QtGui.QLabel(self) self.label.setText(self.otherFile.textas) self.label.setGeometry(200, 200, 60, 40) self.otherFile.setGeometry(100, 100, 200, 200) self.otherFile.setValue(0.5) self.setGeometry(0, 0, 800, 480) self.setWindowTitle('Window Title') def main(): app = QtGui.QApplication(sys.argv) gui = mainWindow() gui.show() sys.exit(app.exec_()) if __name__ == '__main__': main()and here is progress bar code: from PyQt4 import QtGui, Qt, QtCore from PyQt4.Qt import QPen import sys class cPrg(QtGui.QWidget): def __init__(self, initialValue=0, parent=None, *args, **kwargs): super(cPrg, self).__init__(parent, *args, **kwargs) self.linewidth = 1 self.setValue(initialValue) self.textas= 'textas cia' def setValue(self, val): val = float(min(max(val, 0), 1)) self._value = -270 * val self.update() def paintEvent(self, e): print 'yoyo' painter = QtGui.QPainter(self) painter.setRenderHint(painter.Antialiasing) rect = e.rect() gauge_rect = QtCore.QRect(rect) size = gauge_rect.size() pos = gauge_rect.center() gauge_rect.moveCenter( QtCore.QPoint(pos.x()-size.width(), pos.y()-size.height()) ) gauge_rect.setSize(size*.9) gauge_rect.moveCenter(pos) startAngle = 270 * 16 # <-- set start angle to draw arc endAngle = -270 * 16 # <-- set end arc angle painter.setPen(QPen(QtGui.QColor('#000000'), 3))#self.lineWidth)) # <-- arc color # painter.setBrush(QtCore.Qt.HorPattern) painter.drawArc(gauge_rect, startAngle, endAngle) # <-- draw arc painter.end() super(cPrg,self).paintEvent(e)Im not sure what is wrong but thinking something about paintevent (in main.py file this paintevent working). So anyone please can tell me what is wrong? thank you P.S. I know this code is a big mess! :) RE: cant import progress bar from another py file - Yoriz - Dec-17-2016 What is wrong, does the file get imported ok ?, Do you get an error traceback? if so please show it, Does it run with no error but not do what you expect it to do ? if so what do you expect it to do ? and what does it actually do ? I could go on in short you need to give more information to help us help you. RE: cant import progress bar from another py file - swipis - Dec-17-2016 (Dec-17-2016, 10:08 PM)Yoriz Wrote: What is wrong, does the file get imported ok ?, Do you get an error traceback? if so please show it, Does it run with no error but not do what you expect it to do ? if so what do you expect it to do ? and what does it actually do ? I could go on in short you need to give more information to help us help you. Hello Yoriz, there is no error at all. so I have two files - main.py and progressBar.py, as you can guess in progressBar.py there is a simple progress bar painted by paintEvent(). And I trying to import and call this progress bar, and I did this in my other project but in this one I cant get it to work. so there is no errors at all and not sure what to do. RE: cant import progress bar from another py file - Yoriz - Dec-18-2016 Why don't you use the pyqt built in progress bar? On this page zetcode PyQt4 widgets scroll down to QtGui.QProgressBar to see an example of how to use it. RE: cant import progress bar from another py file - swipis - Dec-18-2016 (Dec-18-2016, 01:34 AM)Yoriz Wrote: Why don't you use the pyqt built in progress bar? Because I need circular progress bar for my car project and I why I want to use different file for progress bar - because there will be more then one circular bar and want to keep clean main code :) RE: cant import progress bar from another py file - swipis - Dec-18-2016 I think I figured out why it is not drawing. In main.py file I set a grid (self.grid = QtGui.QGridLayout(self) and then self.grid.addWidget(self.myprogressbar, 1, 0)) and now my progress bar is on screen :) [Image: EIYTW] (image of progress bar) Now question is why I cant set progress bar size in main.py file by using setgeometry? self.myprogressbar.setGeometry(10, 10, 50, 50)here is the progress bar code: class cPrg(QtGui.QWidget): def __init__(self, initialValue=0, parent=None): super(cPrg, self).__init__(parent) self.textas = 'bandom' self.lineWidth = 0 self.startAngle = 0 self.endAngle = 0 self.setValue(initialValue) def setValue(self, val): val = float(min(max(val, 0), 1)) self._value = -270 * val self.update() def setLineWidth(self, lineWidth): self.lineWidth = lineWidth def paintEvent(self, e): painter = QtGui.QPainter(self) painter.setRenderHint(painter.Antialiasing) rect = e.rect() outerRadius = min(self.width(), self.height()) r = QtCore.QRectF(.5,.5,outerRadius-20,outerRadius-20) #<-- create rectangle startAngle = 270 * 16 # <-- set start angle to draw arc endAngle = -270 * 16 # <-- set end arc angle painter.setPen(QPen(QtGui.QColor('#000000'), 3))#self.lineWidth)) # <-- arc color # painter.setBrush(QtCore.Qt.HorPattern) painter.drawArc(r, startAngle, endAngle) # <-- draw arc # arc prg painter.save() painter.setPen(QPen(QtGui.QColor('#ffffff'), 10)) painter.drawArc(r, startAngle, self._value * 16) painter.restore() painter.end() super(cPrg, self).paintEvent(e) RE: cant import progress bar from another py file - Yoriz - Dec-18-2016 So its not a progress bar its a dial gauge, your code look very similar to the code at http://stackoverflow.com/questions/12011147/how-to-create-a-3-color-gradient-dial-indicator-the-one-that-shows-green-yellow but with bits missing/altered. Note there is also a built in dial gauage The call to self.myprogressbardoes not match the assignment of the dial gauge in the code first shown which was self.otherFile RE: cant import progress bar from another py file - swipis - Dec-18-2016 (Dec-18-2016, 10:23 AM)Yoriz Wrote: So its not a progress bar its a dial gauge, your code look very similar to the code at http://stackoverflow.com/questions/12011147/how-to-create-a-3-color-gradient-dial-indicator-the-one-that-shows-green-yellow but with bits missing/altered. Note there is also a built in dial gauage The call toself.myprogressbardoes not match the assignment of the dial gauge in the code first shown which wasself.otherFile Ohh sorry yes this was named self.otherFilenot self.myprogressbar, sorry again. And I did not know about built in dial gauge. My code looks very similar because it is my example how to build one my self :) so any idea why I cant setGeometry of my dial gauge? |