codingJoe
codingJoe

Reputation: 4943

Python Integrating pygame and tk

Does anyone know how to integrate pygame with Tk? I am trying to drive a pygame display with a Tk form and I'm having difficulties. Below is a simple example of the kind of thing I'm trying to accomplish. I'm trying to get input from a Tk form to trigger action in the pygame window. I'm not sure how to get past the basic interaction. Any one done this type of thing? advice?

# The following code has 2 major problems.
#  1.  The window does not refresh when it is dragged over the pygame field.
#  2.  How to plot variables on the screen when the 'Draw' button is clicked?

from Tkinter import *
import os, sys, pygame
from pygame.locals import *

pygame.init()

size = width, height = 1200, 800 
CENTER = width/2, height/2

class Application(Frame):

    def draw_circle(self):
        print "How do I draw a circle at (x,y) radius?"
        print "Does this code belong here?"

    def createWidgets(self):
        myXFrame = Frame(self, bd=2, relief=RIDGE)
        Label(myXFrame, text='X:').pack(side=LEFT, padx=5)
        myX = StringVar()
        Entry(myXFrame, textvariable=myX, bg='white').pack(side=RIGHT, padx=5)
        myX.set('X')
        myXFrame.pack(expand=1, fill=X, pady=10, padx=5)

        myYFrame = Frame(self, bd=2, relief=RIDGE)
        Label(myYFrame, text='Y:').pack(side=LEFT, padx=5)
        myY = StringVar()
        Entry(myYFrame, textvariable=myY, bg='white').pack(side=RIGHT, padx=5)
        myY.set('Y')
        myYFrame.pack(expand=1, fill=X, pady=10, padx=5)

        radiusFrame = Frame(self, bd=2, relief=RIDGE)
        Label(radiusFrame, text='Radius:').pack(side=LEFT, padx=5)
        radius = StringVar()
        Entry(radiusFrame, textvariable=radius, bg='white').pack(side=RIGHT, padx=5)
        radius.set('radius')
        radiusFrame.pack(expand=1, fill=X, pady=10, padx=5)

        self.DRAW = Button(self)
        self.DRAW["text"] = "DRAW"
        self.DRAW["fg"]   = "red"
        self.DRAW["command"] =  self.draw_circle 
        self.DRAW.pack({"side": "left"})

    def __init__(self, master=None):
        Frame.__init__(self, master)
        self.pack()
        self.createWidgets()

def main():

Upvotes: 0

Views: 910

Answers (2)

Ray
Ray

Reputation: 1667

I've tried this. The main idea is to set a timer event using root.after(miliseconds, call_back). This will tirgger the pygame part in your Tk program.

Here's an example.

import pygame
import Tkinter as tk
from random import randint
class MyGame:
    def __init__(self):
        self.root = tk.Tk()
        tk.Button(self.root, text='Click Me', command=self.add_point).pack()

        pygame.display.init()
        self.screen = pygame.display.set_mode((200, 200))
        self.screen.fill(0xffffff)
        pygame.display.flip()

        self.cnt = 0

    def add_point(self, r=20):
        pygame.draw.circle(self.screen, randint(0, 0xffffff), 
                (randint(10, 190), randint(10, 190)), r)
        pygame.display.flip()

    def loop(self):
        # do logic
        # do render
        self.cnt += 1
        if self.cnt % 10 == 0:
            self.add_point(3)
        self.root.after(5, self.loop)

    def mainloop(self):
        self.root.after(5, self.loop)
        self.root.mainloop()

MyGame().mainloop()

Click the button and the pygame window will react you.

Upvotes: 1

gurney alex
gurney alex

Reputation: 13645

have you looked at PyToolkit? It looks like the bridge you're looking for. Hmm, except, no, the download links are dead :-(

Upvotes: 0

Related Questions