Calling Progress bar class from another file

Asked

Viewed 293 times

3

People come to ask for help from you, I believe very simple but that gives a bad headache for those who do not have much practice with object orientation, I have a file progressbar.py, which since the name is already self-explanatory, contains a progressibar in pyqt4, I want to call the file class by passing a title, something like:

 from modules import progressbar 

 progressbar.ProgressBar("TITULO AQUI")

NOTE: As it is an activity bar I do not worry about passing the % progress, I will simply leave it active while a certain process is being run, Eagerly awaiting feedback from you

from PyQt4 import QtCore
from PyQt4 import QtGui
import time
import sys
import qdarkstyle

class ProgressBar(QtGui.QWidget):

    def __init__(self,title):
        super(ProgressBar, self).__init__()
        layout = QtGui.QGridLayout(self)
        self.title = title

        label = QtGui.QLabel()

        label.setText(self.title)
        label.setAlignment(QtCore.Qt.AlignCenter)
        layout.addWidget(label)
        # Create a progress bar and a button and add them to the main layout
        self.progressBar = QtGui.QProgressBar(self)
        self.progressBar.setRange(0,1)
        layout.addWidget(self.progressBar)

        self.myLongTask = TaskThread()
        self.myLongTask.taskFinished.connect(self.onFinished)
        self.onStart()

    def onStart(self):
        self.progressBar.setRange(0,0)
        self.myLongTask.start()

    def onFinished(self):
        # Stop the pulsation
        self.progressBar.setRange(0,1)


class TaskThread(QtCore.QThread):
    taskFinished = QtCore.pyqtSignal()
    def run(self):
        time.sleep(30)
        self.taskFinished.emit()

In my Main Function main.py the part I call progressbar is exactly this :

# -*- coding: utf-8 -*-
import sys, time
from PyQt4.QtGui import  QMessageBox, QMainWindow, QApplication
from PyQt4.uic import loadUi
import qdarkstyle

from .modules import tricks
from .modules import progressbar 


    class PyAuto(QMainWindow):
        def __init__(self):
            super(PyAuto, self).__init__()

            # instances
            self.ui = loadUi('Pyauto/views/menu.ui', self)
            self.ui.test.clicked.connect(self.test_progressbar)
            self.ui.show()

        def test_progressbar(self):

            window = progressbar.ProgressBar("Test")
            window.show()
            app.exec_()



    if __name__ == '__main__':

        app = QApplication(sys.argv)
        app.setStyleSheet(qdarkstyle.load_stylesheet(pyside=False))   # set stylesheet dark
        myWindow = PyAuto()
        app.exec_()
  • Well, you just didn’t share the most important code, which is the class constructor ProgressBar. :) Anyway, you just add the parameter in the constructor (function __init__), the same way you already do in your class MyCustomWidget with the variable title. (if I really understand your difficulty, because the question is not very clear...)

  • Hello friend, put the code, in this test progressbar I will do something like while the process("test") exists: rodeprogressbar("test process running, wait") waiting for feedback, thank you.

  • Ah, your class is ProgressBar is actually called MyCustomWidget! I didn’t understand why you literally call her ProgressBar in an example at the beginning. Okay, but then I didn’t understand your problem/difficulty. You’re already passing a "title" ("Test") on the call of the builder, and inside you are already using it in a label. What’s the matter?

  • the problem, is that if I call the file alone, it works, when I call another class the progressibar, opens and closes at the same instant.

  • P.S.: Be sure to correct the class name in the question (change Progressbar("TITULO AQUI") for MyCustomWidget("TITULO AQUI")). Otherwise you can keep confusing other people. :)

  • maybe something is missing in window.show() app.exec()

  • window.show() only displays the graphical component. You at all times need to have a app.exec() (a single, in the main code) to allow Qt to maintain the application’s main loop. If not, it actually displays and ends.

  • changed the names

  • @Luizvieira but the app.exec_() is in the code, even though it doesn’t run, from the following error: QCoreApplication::exec: The event loop is already running

  • Yes, I tested and saw the error (had nothing to do with the lack of exec). I answered and hope it helps. In the future, try to prepare a [mcve] that reproduces the error. It is much easier for someone to be interested in helping you. : ) Good luck!

  • 1

    @Luizvieira, thank you very much, I will check your example now, this is my first question posted on the stack, I will follow as you said.

Show 6 more comments

1 answer

2


The problem is that you create (instance) your class object ProgressBar within the slot click the button (within the call of test_progressbar). Thus, the scope of this variable is only that function (i.e., it only exists as long as the function exists). As soon as the function ends, the object is deleted and so you see it appear and disappear.

The ideal is to have this instance in a property of its main class (PyAuto). For example:

def __init__(self):
    super(PyAuto, self).__init__()

    # instances
    self.window = progress.ProgressBar("Test") # <==== Adição
    . . . 

And then, at the click of the button, control the display of the existing class:

def test_progressbar(self):
    if not self.window.isVisible():
        self.window.show()
    else:
        self.window.hide()

If you still prefer to instantiate a new class every time you click the button, make sure that it is stored in the main class. Something like:

def __init__(self):
    super(PyAuto, self).__init__()

    # instances
    self.window = None

And then:

def test_progressbar(self):
    self.window = progressbar.ProgressBar("Test")
    self.window.show()

And you don’t need exec, because the call is already being made in the main function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.