Python Forum
migrate code from tkinter to pygame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
migrate code from tkinter to pygame
#10
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.
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
Reply


Messages In This Thread
migrate code from tkinter to pygame - by Frankduc - May-26-2022, 02:40 PM
RE: migrate code from tkinter to pygame - by deanhystad - May-28-2022, 02:20 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame and tkinter seteg 1 1,584 Feb-14-2022, 12:04 PM
Last Post: dboxall123
  Pygame mixer and tkinter music player Kumarkv 1 2,814 May-14-2020, 06:08 PM
Last Post: Larz60+
  How can I use concurrency to migrate database in Python? binhduonggttn 4 2,613 Jan-31-2020, 09:25 AM
Last Post: buran
  Pygame*import pygame ImportError: No module named pygame CASPERHANISCH 1 9,810 Jun-05-2017, 09:50 PM
Last Post: nilamo
  Global Variables. Migrate code from MatLab Felipe 8 7,024 Jan-13-2017, 01:19 AM
Last Post: Felipe

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020