Python Forum
[PyQt] cant import progress bar from another py file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[PyQt] cant import progress bar from another py file
#1
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! :)
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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.
Reply
#5
(Dec-18-2016, 01:34 AM)Yoriz Wrote: 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.

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 :)
Reply
#6
Star 
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)
Reply
#7
So its not a progress bar its a dial gauge, your code look very similar to the code at http://stackoverflow.com/questions/12011...een-yellow but with bits missing/altered.

Note there is also a built in dial gauage

The call to
self.myprogressbar
does not match the assignment of the dial gauge in the code first shown which was
self.otherFile
Reply
#8
(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/12011...een-yellow but with bits missing/altered. Note there is also a built in dial gauage The call to
self.myprogressbar
does not match the assignment of the dial gauge in the code first shown which was
self.otherFile

Ohh sorry yes this was named
self.otherFile
not 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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Import a file and show file name on qcombobox GMCobraz 1 1,924 Jul-02-2020, 01:38 PM
Last Post: GMCobraz
  [PyQt] Import Excel file and use pandas WBPYTHON 2 5,606 Mar-22-2020, 11:28 AM
Last Post: WBPYTHON
  How can I measure progress and display it in the progress bar in the interface? Matgaret 2 5,880 Dec-11-2019, 03:30 PM
Last Post: Denni
  [Tkinter] Progress Bar While Sending an Email maxtimbo 3 3,999 Oct-09-2019, 09:13 PM
Last Post: woooee
  Progress Bar While Sending an Email maxtimbo 0 2,086 Oct-08-2019, 02:13 PM
Last Post: maxtimbo
  GUI Progress Bar Anysja 6 6,532 Aug-29-2018, 02:34 PM
Last Post: swetanjali
  PyQt5 - import rext from other file - despearte for help D_frucht 1 2,447 May-26-2018, 06:37 AM
Last Post: Barrowman

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020