Timer nie działa

0

#!/usr/bin/python3
# -*- coding: utf-8 -*-
from PyQt5.QtWidgets import QMainWindow, QFrame, QDesktopWidget, QApplication
from PyQt5.QtCore import Qt, pyqtSignal
from PyQt5.QtGui import QPainter, QColor 
import sys, random
import timer
import time
import threading 
class Kalkulator(QMainWindow):
   
    def __init__(self, parent=None):
        super().__init__(parent)
        self.interfejs()
        self.foo()
        

    def interfejs(self):
        self.resize(300, 100)
        self.setWindowTitle("Prosty kalkulator")
        self.show()


    def foo(self):   
        self.timer = threading.Timer(2.0, self.timerEvent())
        self.timer.start()

    def timerEvent(self):
        print('xd')

if __name__ == '__main__':
    import sys

    app = QApplication(sys.argv)
    okno = Kalkulator()
    okno.foo()
    sys.exit(app.exec_())
    

Proszę o pomoc bo konsola wywala dziwny błąd, jaka może być przyczyna?

0

Jaki błąd?

0
Dregorio napisał(a):

Jaki błąd?

xd
xd
Exception in thread Thread-2:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 1182, in run
    self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable

Exception in thread Thread-1:
Traceback (most recent call last):
  File "/usr/lib/python3.6/threading.py", line 916, in _bootstrap_inner
    self.run()
  File "/usr/lib/python3.6/threading.py", line 1182, in run
    self.function(*self.args, **self.kwargs)
TypeError: 'NoneType' object is not callable

    
0

1,

 def __init__(self, parent=None):
        super().__init__(parent)

Po co ci ten partent=None? Inicjalizujac pozniej Kalkulator() wywolujesz metode super().__init__(None)

Blad wcale nie jest dziwny - podpowiada ci ze przekazujesz None jako argument funkcji ktory pozniej jest wywolywany.

self.timer = threading.Timer(2.0, self.timerEvent()) zamien self.timerEvent() -> self.timerEvent

Poczytaj troche jak sie dzieje przekazywanie funkcji jako argument, no i tez cos o PEP8 na przyszlosc :D

1 użytkowników online, w tym zalogowanych: 0, gości: 1