May-28-2022, 02:20 PM
(This post was last modified: May-28-2022, 02:20 PM by deanhystad.)
You are trying to make the program work without understanding how all the different parts work. This approach hardly ever works. A carpenter that doesn't know how his tools work will build a crappy house. A programmer that doesn't understand the language or the libraries will write a crappy program.
To write this program you need to know the following:
1 How do I get input from GPIO
2 How do I debounce GPIO inputs
3 How do I map a change in GPIO input to a function
Start with #1.
This program uses the QTimer to update the display once a second. It displays the current data and time as well as the states of button1 and button2. Replace the random number with real code that reads the buttons and see if it displays the current button state. When hooked up to real buttons change the update to 100 times a second.
To write this program you need to know the following:
1 How do I get input from GPIO
2 How do I debounce GPIO inputs
3 How do I map a change in GPIO input to a function
Start with #1.
This program uses the QTimer to update the display once a second. It displays the current data and time as well as the states of button1 and button2. Replace the random number with real code that reads the buttons and see if it displays the current button state. When hooked up to real buttons change the update to 100 times a second.
import PySide6.QtWidgets as QtWidgets # I have PySide6. Change to whatever PySide version you are using. import PySide6.QtCore as QtCore import random from datetime import datetime class Diapo(QtWidgets.QWidget): def __init__(self, parent=None): super().__init__(parent) layout = QtWidgets.QVBoxLayout(self) self.time_label = QtWidgets.QLabel(self) self.button1_label = QtWidgets.QLabel(self) self.button2_label = QtWidgets.QLabel(self) layout.addWidget(self.time_label) layout.addWidget(self.button1_label) layout.addWidget(self.button2_label) self.timer = QtCore.QTimer() self.timer.timeout.connect(self.timerEvent) self.timer.start(1000) def timerEvent(self): self.time_label.setText(datetime.now().strftime("%B %d, %I:%M:%S %p")) button1 = random.randint(0, 1) # replace with code that gets gpio buttons state button2 = random.randint(0, 1) self.button1_label.setText(str(button1)) self.button2_label.setText(str(button2)) app = QtWidgets.QApplication([]) window = Diapo() window.show() app.exec_()Try to figure out how you can modify this program to read the state of your buttons