0

I'm building a boggle game, and I want to add a circle countdown timer to the game. I built a timer using pygame, and the whole game is built on tkinter. Is it possible to merge the two windows so that the timer appears on the game board when the background of the game board is the background of the timer. And that the timer will run along with the game.

This is my timer code- in pygame:

import pygame
import tkinter as tk
class Timer:

    def __init__(self):
        pygame.init()
        self.window = pygame.display.set_mode((200, 200))
        self.clock = pygame.time.Clock()
        self.font = pygame.font.SysFont(None, 30)
        self.sec = 59
        self.counter = 3
        self.text = self.font.render(str(self.sec), True, (0, 128, 0))

        timer_event = pygame.USEREVENT + 1
        pygame.time.set_timer(timer_event, 600)

        def drawArc(surf, color, center, radius, width, end_angle):
            arc_rect = pygame.Rect(0, 0, radius * 2, radius * 2)
            arc_rect.center = center
            pygame.draw.arc(surf, color, arc_rect, 0, end_angle, width)

        run = True
        while run:
            self.clock.tick(60)
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    run = False
                elif event.type == timer_event:
                    if 0 < self.sec < 60:
                        self.sec -= 1
                    else:
                        self.counter -= 1
                        self.sec = 59
                    self.text = self.font.render(str(self.counter) + ":" + str(self.sec), True, (113, 113, 198))
                    if self.sec == 0 and self.counter == 0:
                        pygame.time.set_timer(timer_event, 0)

            self.window.fill((255, 255, 255))
            text_rect = self.text.get_rect(center=self.window.get_rect().center)
            self.window.blit(self.text, text_rect)
            drawArc(self.window, (171, 130, 255), (100, 100), 50, 10, 2 * math.pi * self.sec / 60)
            pygame.display.flip()

This is my main tkinter code-

import tkinter as tk
from PIL import ImageTk
GAME_IMAGE = "game_screen.png"
root = tk.Tk()
canvas = tk.Canvas(root, width=700,
                        height=500, bd=0, highlightthickness=0)
canvas.pack()
tk_img = ImageTk.PhotoImage(file=GAME_IMAGE)
canvas.create_image(400, 270,
                         image=tk_img)
root.mainloop()
  • What have you tried so far? Where do you struggle? You want to embed the Pygame window in a tkinter frame. See [Embedding a Pygame window into a Tkinter frame](https://stackoverflow.com/questions/23319059/embedding-a-pygame-window-into-a-tkinter-or-wxpython-frame) and [I'm embedding a pygame window into Tkinter, how do I manipulate the pygame window?](https://stackoverflow.com/questions/55755305/im-embedding-a-pygame-window-into-tkinter-how-do-i-manipulate-the-pygame-windo) – Rabbid76 Jun 17 '22 at 13:33
  • I do but it is not working for me –  Jun 17 '22 at 13:35
  • I tried this - Embedding a Pygame window into a Tkinter frame –  Jun 17 '22 at 13:36
  • What have you tried so far? where is your attempt? If it doesn't work, you need to show us the code that doesn't work. – Rabbid76 Jun 17 '22 at 13:37
  • If you are using Windows, there is no solution to your problem. See [Using 'SDL_WINDOWID' does not embed pygame display correctly into another application #1574](https://github.com/pygame/pygame/issues/1574). Why might you want to mix Pygame and Tkinter? Why can't you just do it in pygame? The timer code is from here: [How to make a circular countdown timer in Pygame?](https://stackoverflow.com/questions/67168804/how-to-make-a-circular-countdown-timer-in-pygame/67168896#67168896). – Rabbid76 Jun 17 '22 at 13:45
  • yes it is, but I want to combine the pygame window(the timer) with the game window. I need to build a boggle game with tkinter(for school) so I need a timer and I really liked your timer code but I dont know how to place the timer on screen. –  Jun 17 '22 at 13:48
  • But that's exactly the problem. You cannot "combine" a Tkinter window with a Pygame window on Windows. This is a bug in pygame respectively it is not implemented in pygame 2. I implemented a bug fix myself and created a pull request, but it still doesn't merge: https://github.com/pygame/pygame/pull/2981 and https://github.com/pygame/pygame/pull/2982 – Rabbid76 Jun 17 '22 at 13:49
  • ok, can you help me to place a timer on the game board with tkinter?, because I really dont know how to do this. –  Jun 17 '22 at 13:52
  • I need the timer to be a countdown timer start with 3 min. –  Jun 17 '22 at 13:53
  • frankly, as for me mixinig tkinter and pygame looks stupid - it mix of two different worlds. You could do timer in tkinter using `after()` and display as text in `Label` in tkinter, or put directly on `Canvas`. Eventually you could use Pillow to generate image and display image on Canvas. But Canvas should have also function to draw `arc` ([create_arc](https://anzeljg.github.io/rin2/book2/2405/docs/tkinter/create_arc.html)) – furas Jun 17 '22 at 15:16
  • my example code on GitHub: it uses tkinter to draw animated arc on canvas: [furas / python-examples / tkinter / canvas / canvas-arc-draw-clock](https://github.com/furas/python-examples/tree/master/tkinter/__canvas__/canvas-arc-draw-clock) – furas Jun 17 '22 at 15:53

0 Answers0