Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9266759
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T14:23:32+00:00 2026-06-18T14:23:32+00:00

I have a problem with PyQT4 for Python. There is a label with text

  • 0

I have a problem with PyQT4 for Python. There is a label with text and button connected to function. The function could change the text of label first, then call other function. There is a problem with it: the function is executed firts, then change text of the label.

Code:

# -*- coding: utf-8 -*-
import time
import sys
from PyQt4 import QtCore, QtGui

def timesleep():
    print("start sleep")
    time.sleep(5)
    print("stop sleep")



class AnyWidget(QtGui.QWidget):
    def __init__(self,*args):

        QtGui.QWidget.__init__(self,*args)
        self.setWindowTitle("PETHARD")
        boxlay = QtGui.QHBoxLayout(self)
        frame = QtGui.QFrame(self) # Фрейм
        frame.setFrameShape(QtGui.QFrame.StyledPanel)
        frame.setFrameShadow(QtGui.QFrame.Raised)

        gridlay = QtGui.QGridLayout(frame) # Менеджер размещения элементов во фрейме
        label = QtGui.QLabel(u"Welcome",frame) # Текстовая метка.
        global glabel
        glabel = label
        gridlay.addWidget(label,0,0)
        button1 = QtGui.QPushButton(u"Load From MC", frame)
        self.connect(button1, QtCore.SIGNAL("clicked()"), self.ts)
        gridlay.addWidget(button1,1,0)
        boxlay.addWidget(frame)

    def ts(self):
        global glabel
        glabel.setText(u"Waiting...")
        timesleep()

if __name__=="__main__":
    app = QtGui.QApplication(sys.argv)
    aw = AnyWidget()
    aw.show()
    sys.exit(app.exec_())

Help me please to fix this problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T14:23:33+00:00Added an answer on June 18, 2026 at 2:23 pm

    You never want to tie-up your GUI with a long-running function. That the label does not update is only one manifestation of the problem. Even if you get the label to update before the function is called, it will still “freeze” your GUI — no widget will respond to the user until the long-running function completes.

    If you have to run such a function, see if there is a way to break it up into small pieces which each relinquish control to the GUI, or consider using a separate thread to run the function:

    import time
    import sys
    from PyQt4 import QtCore, QtGui
    
    
    class TimeSleep(QtCore.QThread):
        def __init__(self, parent=None):
            QtCore.QThread.__init__(self, parent)
            self.parent = parent
    
        def run(self):
            print("start sleep")
            time.sleep(5)
            print("stop sleep")
            self.parent.glabel.setText(u"Done")
    
    
    class AnyWidget(QtGui.QWidget):
        def __init__(self, *args):
    
            QtGui.QWidget.__init__(self, *args)
            self.setWindowTitle("PETHARD")
            boxlay = QtGui.QHBoxLayout(self)
            frame = QtGui.QFrame(self)  # Фрейм
            frame.setFrameShape(QtGui.QFrame.StyledPanel)
            frame.setFrameShadow(QtGui.QFrame.Raised)
    
            gridlay = QtGui.QGridLayout(
                frame)  # Менеджер размещения элементов во фрейме
            label = QtGui.QLabel(u"Welcome", frame)  # Текстовая метка.
            self.glabel = label
            gridlay.addWidget(label, 0, 0)
            button1 = QtGui.QPushButton(u"Load From MC", frame)
            self.connect(button1, QtCore.SIGNAL("clicked()"), self.ts)
            gridlay.addWidget(button1, 1, 0)
            boxlay.addWidget(frame)
    
        def ts(self):
            self.glabel.setText(u"Waiting...")
            self.thread = TimeSleep(parent=self)
            self.thread.start()
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        aw = AnyWidget()
        aw.show()
        sys.exit(app.exec_())
    

    Here is a wiki page on how to deal with long-running functions.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I`m starting with PyQt4 and right now I have a problem with events. I
I have the following outline of a Python program (using PyQt4): class Polygon( QtGui.QGraphicsItem
I'm using Python 2.6.6 and PyQt4. I have a start QDateTime object and I
I have a Python application (with GUI, using PyQt4) that gets spawned by the
I have a PyQt4 program that I froze using cx_freeze. The problem I am
I have problem with developing chrome-extensions. I have content script: window.addEventListener(load,function(){ var html =
I am writing a GUI program using PyQt4. There is a button in my
Hello I have this problem with PyQt4-dev-tools that include: * a user interface compiler
I have problem with http://abfoodpolicy.com/ . In IE 8 and 9 the right sidebar
I have problem with my query on C, I’m using the oci8 driver. This

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.