使用Tkinter在Pygame中画一个圆 [英] Draw a circle in Pygame using Tkinter

查看:165
本文介绍了使用Tkinter在Pygame中画一个圆的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用另一个问题的一些代码,我在pyinter窗口中嵌入了一个pygame窗口,我试图制作一个在pygame窗口上画一个圆的tkbutton,已经进行了一段时间的实验并且未获得任何结果至今。任何想法都很棒!这是我到目前为止的代码...

  import Tkinter as tk 
import os
import pygame as py

#RGB
红色=(225,0,0)
绿色=(0,255,0)
w,h = 500,200

p =假

def maketrue(p):
p =真实
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()

而True:
如果p == True:
py.draw.circle(screen,red,(250,50),20)
py.display .update()
else:
通过
py.draw.circle(screen,green,(250,100),20)

root.update()


解决方案

本来打算做一段时间,但是我现在有一段时间,这是一些基本代码,该程序创建一个tkinter窗口,然后将pygame窗口嵌入一个框架中,然后再创建另一个框架并放置一个

  import pygame 
从Tkinter导入中以tk
的形式导入Tkinter *
导入os


根= tk.Tk()

嵌入= tk.Frame(root,width = 500,height = 500)#为pygame窗口创建嵌入框架
embed.grid(columnspan =(600),rowspan = 500)#添加网格
embed.pack( side = LEFT)#packs window to 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'

屏幕= pygame.display .set_mode((500,500))
screen.fill(pygame.Color(255,255,255))

pygam e.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()

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


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

解决方案

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

这篇关于使用Tkinter在Pygame中画一个圆的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆