有誰知道如何將pygame與Tk集成?我正嘗試用Tk形式驅動pygame顯示器,並且遇到困難。下面是我試圖完成的一種簡單的例子。我試圖從Tk表單輸入觸發pygame窗口中的動作。我不知道如何克服基本的互動。任何人做這種類型的事情?建議?Python集成pygame和tk
# 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():
你似乎錯過的'身體高清的main():' – Ikke