0

I want to print all events that happen in my application. I am using python 3 with pygame and I use tkinter.

import pygame
from tkinter import *
from tkinter import ttk

def main():            

    root = Tk()
    app = soundscene(root)
    while True:
        for event in pygame.event.get():
           print (event)
    root.mainloop()


if __name__ == "__main__":
    main()

This code does not run as expected because the loop blocks, which makes sense My question is how do I make the content of the "while true" loop part of the eventloop, so it prints out all pygame events?

Zekii
  • 42
  • 6
Nivatius
  • 260
  • 1
  • 13

1 Answers1

1

To receive pygame events, a pygame window / display must be initialized.

"The input queue is heavily dependent on the pygame display module. If the display has not been initialized and a video mode not set, the event queue will not really work." Pygame event documentation.

However, if you do want to use tkinter with pygame, you can embed a pygame window into a tkinter window. This however will only give pygame events when using the embedded window. Here is an example from another question:

import pygame
import tkinter as tk
from tkinter import *
import os

root = tk.Tk()
embed = tk.Frame(root, width = 500, height = 500) #creates embed frame    for pygame window
embed.grid(columnspan = (600), rowspan = 500) # Adds grid
embed.pack(side = LEFT) #packs window to the left
buttonwin = tk.Frame(root, width = 75, height = 500)
buttonwin.pack(side = LEFT)
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
os.environ['SDL_VIDEODRIVER'] = 'windib'
screen = pygame.display.set_mode((500,500))
screen.fill(pygame.Color(255,255,255))
pygame.display.init()
pygame.display.update()

while True:
    for event in pygame.event.get():
        print(event)
    pygame.display.update()
    root.update()

If you are looking for a way to receive events when using tkinter widgets, you can setup bindings in tkinter. More information at this link

Zekii
  • 42
  • 6
  • thank you for taking the time to answer, I forgot to mention that I am only concerned with the pygame.mixer.Sound modul and it's events https://www.pygame.org/docs/ref/mixer.html#pygame.mixer.Sound I am not sure if I also need a frame for that. soundplayback works just as expected. – Nivatius May 23 '18 at 15:12