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
#11
I am sorry i dont see the point of this exercize. Its not working anyway :

gpio.board not define

the button are working in timerEven. i just want to get rid of the button in the QPushButton window so it start automatically. I need to create different files like i said for class and methods. I am not gonna redo this again. I have tons of homeworks i already passing the course its got to be quick and simple. This college suck anyway to learn programming, we are not learning anything.

I tried with the other code you posted and the problem still the same:
I really dont know how to do this.

import tkinter as tk
import itertools
import datetime as DT
import time
import RPi.GPIO as GPIO
from time import sleep
import datetime
import time
import yaml
#fichier yaml configuration de l'intervalle
fichier = open("diaporama.yaml", "r")
document = yaml.load(fichier, Loader=yaml.Loader)
print(document["heureA"])
print(document["heureD"])
#fichier yaml configuration du choix d'images
with open('voilier.yaml') as y:
    for ligne in y:
        print(ligne)
with open('yatch.yaml') as yatch:
    for y in yatch:
        print(y)
#connection Raspberry Pi
GPIO.setmode(GPIO.BOARD)
GPIO.setup(document["bouton1"],GPIO.IN,pull_up_down=GPIO.PUD_UP)
GPIO.setup(document["bouton2"],GPIO.IN,pull_up_down=GPIO.PUD_UP)
dateSTR = datetime.datetime.now().strftime("%H:%M:%S" )
 
images = ['/home/pi/Desktop/jpg2png/athenaR.png',
'/home/pi/Desktop/jpg2png/EosR.png',
'/home/pi/Desktop/jpg2png/k.png',
'/home/pi/Desktop/jpg2png1/athena1.png',
'/home/pi/Desktop/jpg2png1/meteor.png',
'/home/pi/Desktop/jpg2png1/tshenandoah.png',
'/home/pi/Desktop/jpg2png1/Wolfhound.png',
'/home/pi/Desktop/jpg2png1/Aunderway.png']
 
image_files1 = [
    '/home/pi/Desktop/jpg2png2/Ar.png',
'/home/pi/Desktop/jpg2png2/AntibeR.png',
'/home/pi/Desktop/jpg2png2/dubai.png',
'/home/pi/Desktop/jpg2png2/KHALILAHR.png',
'/home/pi/Desktop/jpg2png2/luxuy-yachtR.png',
'/home/pi/Desktop/jpg2png2/turquoiseR.png',
]
 
class ImageViewer(tk.Tk):
    """A tkinter window that displays images like a slideshow"""
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # Label to display the image
        self.image_label = tk.Label(self)
        self.image_label.pack()
        # Label to display the image file name
        self.name_label = tk.Label(self, width=30)
        #self.name_label.pack()
        self.image = None
        self.delay = 2000
        self.running = False
    
 
    def start(self, image_files, loop=True, delay=None):
        """
        Start slide show.  Set loop to True to cycle through images forever.
        Set delay to seconds that image is shown.
        """
        if loop:
            """Cycle forever"""
            #self.image_files = itertools.cycle(image_files)
            if GPIO.input(document["bouton1"])==1:
                self.image_files1 = itertools.cycle(images)
            if GPIO.input(document["bouton1"])==0:
                self.image_files1 = itertools.cycle(image_files1)
        else:
            """Show slides once"""
            #self.image_files = iter(image_files)
            self.image_files1 = iter(image_files1)
        if delay is not None:
            self.delay = delay * 1000
        self.running = True
        self.update_image()
 
    def stop(self):
        """Stop slideshow"""
        self.running = False
 
    def update_image(self):
        """Show the next image"""
        if self.running:
            try:
                image_file = next(self.image_files1)
                self.name_label["text"] = image_file
                self.image = tk.PhotoImage(file=image_file)
                self.image_label["image"] = self.image
                self.after(self.delay, self.update_image)
            except StopIteration:
                self.running = False
 
 
viewer = ImageViewer()
viewer.start(images, loop=True, delay=1)
 
viewer.mainloop()
Reply
#12
The point of the exercise is to learn how you can use hard buttons to control a GUI program.
Reply
#13
I understand that but if you dont know what you need or how to do you it, you are just wasting time. I just try to get rid of the button so the images are iterating like with iter in Tkinter. Its been an hour i am looking for a solution on the web. I studied in social science and also in finance before trying programming. I find teachings in programming poor its like we let students learn by them self and the teachers are useless. Is it a standard in the industry?
Maybe i am complaining to much but most students i spoke with feel the same.
Reply
#14
If you switch between images and images1 it makes more sense to make a list of image lists.
image_files = [
    [   '/home/pi/Desktop/jpg2png/athenaR.png',
        '/home/pi/Desktop/jpg2png/EosR.png',
        '/home/pi/Desktop/jpg2png/k.png',
        '/home/pi/Desktop/jpg2png1/athena1.png',
        '/home/pi/Desktop/jpg2png1/meteor.png',
        '/home/pi/Desktop/jpg2png/falconR.png',
        '/home/pi/Desktop/jpg2png/seacloudR.png',
        '/home/pi/Desktop/jpg2png/SeagleR.png'],

    [   '/home/pi/Desktop/jpg2png2/Ar.png',
        '/home/pi/Desktop/jpg2png2/AntibeR.png',
        '/home/pi/Desktop/jpg2png2/dubai.png',
        '/home/pi/Desktop/jpg2png2/KHALILAHR.png',
        '/home/pi/Desktop/jpg2png2/luxuy-yachtR.png',
        '/home/pi/Desktop/jpg2png2/turquoiseR.png']
]
Now you can have any number of photo albums that the user can select from. When the user presses button2, just select the next group of images.

I don't understand your project all that well so I made one that acts like an slide carousel. Press button 1 to advance to the next slide. Press button 2 to switch to a different group of slides.
import tkinter as tk
import RPi.GPIO as GPIO
import yaml
import itertools

image_files = [
    [   '/home/pi/Desktop/jpg2png/athenaR.png',
        '/home/pi/Desktop/jpg2png/EosR.png',
        '/home/pi/Desktop/jpg2png/k.png',
        '/home/pi/Desktop/jpg2png1/athena1.png',
        '/home/pi/Desktop/jpg2png1/meteor.png',
        '/home/pi/Desktop/jpg2png/falconR.png',
        '/home/pi/Desktop/jpg2png/seacloudR.png',
        '/home/pi/Desktop/jpg2png/SeagleR.png'],

    [   '/home/pi/Desktop/jpg2png2/Ar.png',
        '/home/pi/Desktop/jpg2png2/AntibeR.png',
        '/home/pi/Desktop/jpg2png2/dubai.png',
        '/home/pi/Desktop/jpg2png2/KHALILAHR.png',
        '/home/pi/Desktop/jpg2png2/luxuy-yachtR.png',
        '/home/pi/Desktop/jpg2png2/turquoiseR.png']
]

class ImageViewer(tk.Tk):
    """A tkinter window that displays images like a slideshow"""
    def __init__(self, hardware_config, all_images):
        super().__init__()
        self.image_label = tk.Label(self)
        self.image_label.pack()

        # Load config?  Are the button ID's the only thing in the file?
        with open(hardware_config, "r") as file:
            document = yaml.load(file, Loader=yaml.Loader)
            self.button1 = document["bouton1"]
            self.button2 = document["bouton2"]

        #connection Raspberry Pi
        GPIO.setmode(GPIO.BOARD)
        GPIO.setup(self.button1, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        GPIO.setup(self.button2, GPIO.IN, pull_up_down=GPIO.PUD_UP)
        self.button1_state = 0
        self.button2_state = 0

        # Load the images.  Images are grouped into folders which the user
        # can switch between by pressing button2
        self.folders = itertools.cycle(all_images)
        self.next_folder()

        self.read_buttons()

    def next_image(self):
        """Display the next image"""
        self.image = tk.PhotoImage(file=next(self.images))
        self.image_label["image"] = self.image

    def next_folder(self):
        """Select the next image folder/group"""
        self.images = itertools.cycle(next(self.folders))
        self.next_image()
  
    def read_buttons(self):
        """Check if a button is pushed.  Pushing button 1 advances
        to the next slide.  Pushing button two switches to the next
        group of slides (folder)"""

        # button 1 loads the next image
        button_state = GPIO.input(self.button1)
        if button_state != self.button1_state:
            # Only advance 1 image per button press
            self.button1_state = button_state
            if self.button1_state == 1:
                self.next_image()

        # button 2 switches the image folder
        button_state = GPIO.input(self.button2)
        if button_state != self.button2_state:
            # Only advance 1 folder per button press
            self.button2_state = button_state
            if self.button2_state == 1:
                self.next_folder()

        self.after(10, self.read_buttons)
  
ImageViewer("diaporama.yaml", image_files).mainloop()
Reply
#15
Hello Dean,

As usual you came up with a solution i could never reproduce myself. That is frustrating cause they did not teach us how to use those super init and this is a complete different way to do that ImageViewer("diaporama.yaml", image_files).mainloop() and it would of never come to my mind. Students are just doing like me they take code on web and try to modify them to fit the needs of the homework.

I am trying to add to your work the self.delay = 2000. So the image slide one after another. The project is you click bouton1 to switch album and click button 2 to pause the image.

Anyway thank you for your help.
Reply
#16
In your Python class where you are asked to use classes, the teacher has never discussed super()? super() is the recommended way to call a method defined by the superclass, usually the __init__() method. I am positive that many examples you've seen online used super(). It is not an obscure language feature.

ImageViewer("diaporama.yaml", image_files) passes arguments to the ImageViewer.__init__() method. There is nothing unusual about that. I think most classes accept arguments in their __init__() method.

root_window.mainloop() appears in every tkinter program. ImageViewer is a subclass of Tk. Tk() returns a root window, so ImageViewer() returns a root window too. Thus:
ImageViewer("diaporama.yaml", image_files).mainloop()
is exactly the same as:
root = ImageViewer("diaporama.yaml", image_files)
root.mainloop()
which is only a "classy" version of:
root = Tk()
root.initialize("diaporama.yaml", image_files)
root.mainloop()
Reply
#17
The first course in python cover the book Python 3 pour tous by Stephane Gill you can find it online for free. He covered _init_ a little bit but that's it. I feel unprepared to achieve those assigments and i am wondering if its me whose not looking enough on the web to research this or it should be teached in class. Sometimes you just dont know what key word use or how to explain a concept to solve a problem. We have 4 courses and 2 to 3 weeks to achieve the homework with 25 hours of class per week. One would wonder what a course of infographics or sql are doing in AI.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pygame and tkinter seteg 1 1,507 Feb-14-2022, 12:04 PM
Last Post: dboxall123
  Pygame mixer and tkinter music player Kumarkv 1 2,745 May-14-2020, 06:08 PM
Last Post: Larz60+
  How can I use concurrency to migrate database in Python? binhduonggttn 4 2,549 Jan-31-2020, 09:25 AM
Last Post: buran
  Pygame*import pygame ImportError: No module named pygame CASPERHANISCH 1 9,763 Jun-05-2017, 09:50 PM
Last Post: nilamo
  Global Variables. Migrate code from MatLab Felipe 8 6,865 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