Python集成pygame和tk

时间:2011-03-14 01:36:08

标签: python pygame tk

有谁知道如何将pygame与Tk集成?我试图用Tk形式驱动一个pygame显示器,我遇到了困难。下面是一个我想要完成的事情的简单例子。我试图从Tk表单获取输入以触发pygame窗口中的操作。我不确定如何通过基本的互动。有人做过这种事吗?建议?

# 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():

2 个答案:

答案 0 :(得分:1)

我试过这个。主要思想是使用root.after(miliseconds, call_back)设置计时器事件。这将在你的Tk程序中使用pygame部分。

以下是一个例子。

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()

点击按钮,pygame窗口会对你做出反应。

答案 1 :(得分:0)

你看过PyToolkit了吗?它看起来像你正在寻找的桥梁。嗯,除了,不,下载链接已经死了: - (