title: Start/pause pygame via tkinter button in the same window
body: <p>For a school project, a friend and I do a game in pygame. The pygame window is embedded in a tkinter frame. As soon as you open the application the game starts immediately. Now we want to start and pause the game with buttons from tkinter. I'm glad for any help, thanks.</p>
  
  <pre><code>import pygame, random, sys
  from pygame.locals import *
  from pygame.key import *
  import tkinter as tk
  from tkinter import *
  import os
  </code></pre>
  
  <p>Creates a tkinter frame</p>
  
  <pre><code>root = tk.Tk()
  root.title("Borderflip")
  embed = tk.Frame(root, width = 500, height = 500) #Erzeugt einen 
  eingebetteten Rahmen für das Pygame-Fenster
  embed.grid(columnspan = (600), rowspan = 500) 
  embed.pack(side = LEFT) #Fenster nach rechts
  buttonwin = tk.Frame(root, width = 75, height = 500)
  buttonwin.pack(side = LEFT)
  </code></pre>
  
  <p>Here are the Buttons the
  play and pause button is without a function</p>
  
  <pre><code>def play_button():
  
  play_button = Button(root,  padx=8, width=18, pady=8, bd=8, font=("Arial", 
  26), text="Play", command=play_button)
  play_button.pack()
  
  pause_button = Button(root,  padx=8, width=18, pady=8, bd=8, font=("Arial", 
  26), text="Pause", command=root.quit)
  pause_button.pack()
  
  quit_button = Button(root, padx=8, width=18, pady=8, bd=8, font=("Arial", 
  26), text="Quit", command=root.destroy)
  quit_button.pack()
  </code></pre>
  
  <p>Code for integration of pygame in tkinter</p>
  
  <pre><code>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()
  frame = Frame(root)
  frame.pack()
  </code></pre>
  
  <p>Code for the pygame</p>
  
  <pre><code>x = 500 # Breite und Höhe Fenster in Pixel
  y = 500
  
  sbreite = 100 # Breite und Höhe Schläger
  shöhe = 15
  
  sx = 200 # Definition 
  sy = 450
  
  bx = int(x/2)
  by = int(y/2)
  
  brad = 15
  
  speed = 0
  
  bxspeed = 1
  byspeed = -2
  
  leben = 10
  
  def sblock():
      global speed
      if sx &lt;= 0 or sx &gt;= x-sbreite:
          speed = 0
  
  def ballbewegung():
      global bx,by
      bx += bxspeed
      by += byspeed
  
  
  def reset():
      global byspeed,bxspeed,leben,bx,by,sx,sy,speed
      sx = 200
      sy = 450
  
      bx = int(x/2)
      by = int(y/2)
  
  speed = 0
  
  bxspeed = random.randint(-2,2)
  if bxspeed == 0:
      bxspeed = 1
  byspeed = random.randint(-2,2)
  if byspeed == 0:
      byspeed = 2
  screen.fill((0,0,0))
  pygame.draw.circle(screen, (255,255,0), (bx,by), brad, 0)
  pygame.draw.rect(screen, (255,40,0), (sx,sy,sbreite,shöhe), 0)
  pygame.display.flip()
  pygame.time.wait(1000)
  root.update()
  
  def ballblock():
      global byspeed, bxspeed, leben
      if by-brad &lt;= 0:
          byspeed *= -1
      if bx-brad &lt;= 0:
          bxspeed *= -1
      if bx+brad &gt;= x:
          bxspeed *= -1
      if by &gt;= 435 and by &lt;= 440:
          if bx &gt;= sx-15 and bx &lt;= sx + sbreite + 15:
              byspeed *= -1
          else:
              leben -= 1
              reset()
      root.update()
  
  def sbewegung():
      global sx
      sx += speed
  
  while leben &gt; 0:
      for event in pygame.event.get():
          if event.type == pygame.QUIT: sys.exit()
          if event.type == pygame.KEYDOWN:
              if event.key == pygame.K_LEFT:
                  speed = -2
              if event.key == pygame.K_RIGHT:
                  speed = 2
  screen.fill((0,0,0))
  sbewegung()
  sblock()
  pygame.draw.rect(screen, (255,40,0), (sx,sy,sbreite,shöhe), 0)
  
  ballbewegung()
  ballblock()
  pygame.draw.circle(screen, (255,255,0), (bx,by), brad, 0)
  
  pygame.display.flip()
  pygame.time.wait(5)
  
  print ("haha verloren")
  
  while True:
      pygame.display.update()
      root.update()
  </code></pre>