PyQt Application Main Loop?

Post here if you need help with creating a Graphical User Interface in Python.

PyQt Application Main Loop?

Postby Yiab » Fri Apr 04, 2014 2:54 pm

I'm not getting any errors, but my code still isn't working.
I'm trying to write a small email checking client which will pop up a window in the lower right corner when there's new mail, and where the window will go away when it is clicked on.

Code: Select all
def main():

So NotifyWindow is just a QWidget containing a single button which holds the text I want to display. When I test it out in the shell, it displays and vanishes when it should, with the text it should. keep_checking_gmail does what its name suggests, and it tells the NotifyWindow when and what to display. When I run this main method, it all seems to work correctly until it actually gets its first new email registered, then the NotifyWindow tries to show up, but never actually gets painted to the screen (it's all white) and I get an hourglass when I hover the pointer over the NotifyWindow. This continues (with the thread still chugging along in the background) until I kill the process or the program simply exits without an error message.

While I don't really know where in my code the problem is, I'm displaying the main method since that's almost the only thing that's run outside of the keep_checking_gmail function. Any help would be appreciated.

I'm using PyQt5 for Python 3.3 and Windows 7.

Note: I am terrible at GUI programming in any language. Please forgive me if this is a really stupid error on my part.
Last edited by stranac on Fri Apr 04, 2014 2:57 pm, edited 1 time in total.
Reason: First post lock.
Posts: 3
Joined: Fri Apr 04, 2014 2:41 pm

Re: PyQt Application Main Loop?

Postby Mekire » Fri Apr 04, 2014 5:11 pm

First and foremost. Stop using star imports. Just in those three lines it is clear you have star imported at least 3 modules. This truly makes your code unreadable for anyone that doesn't know all modules you are using inside out.

Secondly, you should post more of your program. It should be a testable sample. If this requires you to post your whole program, so be it.
It seems your main problem is you never called but that is my only guess for the moment.

New Users, Read This
  • Use code tags when posting code.
  • Include any errors with your post (in code tags).
  • Describe your problem; not your chosen solution.
  • Make examples the minimum length to demonstrate your issue.
User avatar
Posts: 1367
Joined: Thu Feb 07, 2013 11:33 pm
Location: Tucson, Arizona

Re: PyQt Application Main Loop?

Postby Yiab » Fri Apr 04, 2014 7:37 pm

You are correct that I'm using star imports, though this is because I am totally unfamiliar with PyQt and have copied the basic structure from which I'm working from some sample code found through Google.

The main reason I didn't post the full code is because I read on the "what to do/what not to do" for these forums that it should be a minimal amount of code. But anyway, here it is.

Code: Select all
from usblamp import USBLamp, Color
from imaplib import IMAP4_SSL
from pickle import load, dump
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from time import sleep
from sys import argv, exit
from threading import Thread, RLock, Event
import ctypes, ctypes.wintypes


class NotifyWindow(QWidget):
   def __init__(self,parent=None):

   def show_window(self):
      if not self.is_idle:

   def unset_idle(self):
      with self.win_lock:
         if self.is_showing:

   def set_idle(self):
      with self.win_lock:

   def wasClicked(self):
      with self.win_lock:

   def update_text(self,text):
      with self.win_lock:

def get_login_info():
   return load(open(CREDENTIAL_FILE,"rb"))

def get_seen_emails():
   return load(open(SEEN_EMAIL_FILE,"rb"))

def set_seen_emails(seen_emails):

def check_gmail(window=None):
   for eid in email_ids:
      if eid not in old_email_ids:
         _,ans=imap_server.uid('fetch',eid,'(BODY.PEEK[HEADER.FIELDS (From Subject)] RFC822.SIZE)')
         if window!=None:
   print("step 0")

def stop():

def should_stop():
   return __stop_var.is_set()

class LASTINPUTINFO(ctypes.Structure):
   _fields_ = [
      ('cbSize', ctypes.wintypes.UINT),
      ('dwTime', ctypes.wintypes.DWORD),

user32 = ctypes.windll.user32
GetLastInputInfo = user32.GetLastInputInfo
GetLastInputInfo.restype = ctypes.wintypes.BOOL
GetLastInputInfo.argtypes = [PLASTINPUTINFO]
kernel32 = ctypes.windll.kernel32
GetTickCount = kernel32.GetTickCount
Sleep = kernel32.Sleep

def keep_checking_gmail(window=None):
   while not should_stop():
      if counter==0:
      print("step 1")
      print("step 2")
      if is_idle:
         if elapsed>IDLE_TIME*1000:
            if window!=None:
      elif elapsed<IDLE_TIME*1000:
         if window!=None:
      print("step 3")

def main():

def main_thread():

if __name__=="__main__":

As you can see, is called via the thread I started in the main method, when there's email to actually show.
Posts: 3
Joined: Fri Apr 04, 2014 2:41 pm

Re: PyQt Application Main Loop?

Postby Yiab » Tue Apr 08, 2014 5:11 pm

Okay, here's a much smaller example which fails in the same way and includes all the package names.

Code: Select all
import PyQt5.QtWidgets
import PyQt5.QtCore
import PyQt5.QtGui
import sys
import time
import threading


class ButtonWindow(PyQt5.QtWidgets.QWidget):
   def __init__(self,parent=None):
      self.button=PyQt5.QtWidgets.QPushButton("Push This Button")

   def wasClicked(self):

def show_window(window=None):
   while not __stop_var.is_set():
      if window!=None:

def main():

if __name__=="__main__":

I assume that I'm doing something that's obviously stupid, but I can't see what it might be. Can anybody help?
Posts: 3
Joined: Fri Apr 04, 2014 2:41 pm

Return to GUI

Who is online

Users browsing this forum: No registered users and 1 guest