Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
379 views
in Technique[技术] by (71.8m points)

python - Draw a circle in Pygame using Tkinter

using bits of code from another question, I embedded a pygame window in a tkinter window, I'm trying to make a tkbutton that draws a circle on the pygame window, been experimenting for a while and haven't turned any results so far. Any ideas would be great! Here's the code I have so far...

import Tkinter as tk
import os
import pygame as py

#         R   G  B
red =   (225, 0, 0)
green = (0, 255, 0)
w, h = 500, 200

p = False

def maketrue(p):
    p = True
    returnp

root = tk.Tk()
window = tk.Frame(root, width=w, height=h)
window.pack()

os.environ['SDL_WINDOWID'] = str(window.winfo_id())

root.update

py.display.init()
screen = py.display.set_mode((w, h))
screen.fill(py.Color(255, 0, 0))
drawbutton = tk.Button(root, text='Draw Circle', command = maketrue(p))
drawbutton.pack()

while True:
    if p == True:
        py.draw.circle(screen, red, (250, 50), 20)
        py.display.update()
    else:
        pass
    py.draw.circle(screen, green, (250, 100), 20)

    root.update()
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Been meaning to do this for a while, but I had time now, this is some basic code, the program makes a tkinter window and then embeds a pygame window in a frame, it then makes another frame and puts a button on that window that when pressed, calls a function that tells pygame to draw a circle on the pygame window.

import pygame
import Tkinter as tk
from Tkinter import *
import os


root = tk.Tk()

embed = tk.Frame(root, width = 500, height = 500) #creates embed frame for pygame window
embed.grid(columnspan = (600), rowspan = 500) # Adds grid
embed.pack(side = LEFT) #packs window to the left

buttonwin = tk.Frame(root, width = 75, height = 500)
buttonwin.pack(side = LEFT)

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


def draw():
    pygame.draw.circle(screen, (0,0,0), (250,250), 125)
    pygame.display.update()


button1 = Button(buttonwin,text = 'Draw',  command=draw)
button1.pack(side=LEFT)

root.update()

while True:
    pygame.display.update()
    root.update()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...