Python集成了pygame和

2024-10-06 07:00:51 发布

您现在位置:Python中文网/ 问答频道 /正文

有人知道如何将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():

Tags: thetextselfdefframepygamesidepack
2条回答

你看过PyToolkit了吗?看起来像你要找的那座桥。嗯,除了,不,下载链接是死的:-(

我试过了。主要思想是使用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窗口将对您做出反应。在

相关问题 更多 >