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.
import pygame, random, sys
from pygame.locals import *
from pygame.key import *
import tkinter as tk
from tkinter import *
import os
Creates a tkinter frame
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)
Here are the Buttons the play and pause button is without a function
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 for integration of pygame in tkinter
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 for the pygame
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 <= 0 or sx >= 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 <= 0:
byspeed *= -1
if bx-brad <= 0:
bxspeed *= -1
if bx+brad >= x:
bxspeed *= -1
if by >= 435 and by <= 440:
if bx >= sx-15 and bx <= sx + sbreite + 15:
byspeed *= -1
else:
leben -= 1
reset()
root.update()
def sbewegung():
global sx
sx += speed
while leben > 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()
Tkinter + Pygame don't work well together sadly, usually due to conflicts between their graphics libraries and how one program sleeps when the other is active.
I would personally recommend removing tkinter from your project and building a button
class through pygame.sprite.Sprite()
, which can then be used in the same way a tkinter button would. You can read the documentation on that Here.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.