pythontkinter绘图程序
我有两个独立的程序,都是从堆栈溢出中得到的,都是独立工作的。以下是第一点:pythontkinter绘图程序,python,user-interface,python-3.x,tkinter,pygame,Python,User Interface,Python 3.x,Tkinter,Pygame,我有两个独立的程序,都是从堆栈溢出中得到的,都是独立工作的。以下是第一点: import tkinter as tk import os w, h = 500, 200 # Add a couple widgets. We're going to put pygame in `embed`. root = tk.Tk() embed = tk.Frame(root, width=w, height=h) embed.pack()
import tkinter as tk
import os
w, h = 500, 200
# Add a couple widgets. We're going to put pygame in `embed`.
root = tk.Tk()
embed = tk.Frame(root, width=w, height=h)
embed.pack()
text = tk.Button(root, text='Blah.')
text.pack()
# Tell pygame's SDL window which window ID to use
os.environ['SDL_WINDOWID'] = str(embed.winfo_id())
# The wxPython wiki says you might need the following line on Windows
# (http://wiki.wxpython.org/IntegratingPyGame).
#os.environ['SDL_VIDEODRIVER'] = 'windib'
# Show the window so it's assigned an ID.
root.update()
# Usual pygame initialization
import pygame as pg
pg.display.init()
screen = pg.display.set_mode((w,h))
pos = 0
while 1:
# Do some pygame stuff
screen.fill(pg.Color(0,0,0))
pos = (pos + 1) % screen.get_width()
pg.draw.circle(screen, pg.Color(255,255,255), (pos,100), 30)
# Update the pygame display
pg.display.flip()
# Update the Tk display
root.update()
该程序应将pygame窗口嵌入到tkinter框架中,它的工作方式就像一个符咒,下面是第二个程序:
import pygame, random
screen = pygame.display.set_mode((800,600))
draw_on = False
last_pos = (0, 0)
color = (0, 0, 0)
white = (255,255,255)
radius = 10
screen.fill(white)
def roundline(srf, color, start, end, radius=1):
dx = end[0]-start[0]
dy = end[1]-start[1]
distance = max(abs(dx), abs(dy))
for i in range(distance):
x = int( start[0]+float(i)/distance*dx)
y = int( start[1]+float(i)/distance*dy)
pygame.draw.circle(srf, color, (x, y), radius)
try:
while True:
e = pygame.event.wait()
if e.type == pygame.QUIT:
raise StopIteration
if e.type == pygame.MOUSEBUTTONDOWN:
pygame.draw.circle(screen, color, e.pos, radius)
draw_on = True
if e.type == pygame.MOUSEBUTTONUP:
draw_on = False
if e.type == pygame.MOUSEMOTION:
if draw_on:
pygame.draw.circle(screen, color, e.pos, radius)
roundline(screen, color, e.pos, last_pos, radius)
last_pos = e.pos
pygame.display.flip()
except StopIteration:
pass
pygame.quit()
第二个应该显示一个pygame屏幕,你可以在上面画任何你喜欢的东西
我不是一个非常有经验的pygame程序员,但是,我有使用tkinter的经验。我想做的是做一个程序,你可以在上面画画,但它也必须有tkinter按钮,entry等等
两个程序单独运行都很好,但是,当我想替换第一个程序中的pygame部分时,一切都很好,除了,我不能绘制任何东西,按钮不想被点击,我不能通过x退出,这一点都没有意义,所以我想可能出了什么问题,我找不到这个问题,所以我非常感谢你的建议。我自己已经找到了答案,我使用pygame的原因是因为我不认为你可以使用tkinter,但我制作了一个使用tkinter的程序,它实际上画得非常好(可能比pygame更好),我将添加这个程序
from tkinter import *
b1 = "up"
xold, yold = None, None
display_width = '500'
display_height = '500'
canvas_width = '500'
canvas_height = '500'
def main():
root = Tk()
root.geometry((display_width+"x"+display_height))
drawing_area = Canvas(root,width=canvas_width,height=canvas_height,bg="white")
drawing_area.bind("<Motion>", motion)
drawing_area.bind("<ButtonPress-1>", b1down)
drawing_area.bind("<ButtonRelease-1>", b1up)
drawing_area.pack(side=RIGHT)
root.mainloop()
def b1down(event):
global b1
x1, y1 = ( event.x - 4 ), ( event.y - 4 )
x2, y2 = ( event.x + 4 ), ( event.y + 4 )
event.widget.create_oval( x1, y1, x2, y2, fill = "black" )
b1 = "down" # you only want to draw when the button is down
# because "Motion" events happen -all the time-
def b1up(event):
global b1, xold, yold
b1 = "up"
xold = None # reset the line when you let go of the button
yold = None
def motion(event):
if b1 == "down":
global xold, yold
x1, y1 = ( event.x - 4 ), ( event.y - 4 )
x2, y2 = ( event.x + 4 ), ( event.y + 4 )
event.widget.create_oval( x1, y1, x2, y2, fill = "black" )
if xold is not None and yold is not None:
python_green = "#476042"
x1, y1 = ( event.x - 4 ), ( event.y - 4 )
x2, y2 = ( event.x + 4 ), ( event.y + 4 )
event.widget.create_oval( x1, y1, x2, y2, fill = "black" )
event.widget.create_line(xold,yold,event.x,event.y,smooth=TRUE,width=9)
# here's where you draw it. smooth. neat.
xold = event.x
yold = event.y
if __name__ == "__main__":
main()
从tkinter导入*
b1=“向上”
xold,yold=None,None
显示宽度='500'
显示高度='500'
画布宽度='500'
画布高度='500'
def main():
root=Tk()
根几何体((显示宽度+x+显示高度))
绘图区域=画布(根,宽度=画布宽度,高度=画布高度,bg=“白色”)
绘图区域绑定(“,运动)
绘图区域绑定(“,b1向下)
绘图区域绑定(“,b1up)
绘图区域包装(侧面=右侧)
root.mainloop()
def b1down(事件):
全球b1
x1,y1=(事件x-4),(事件y-4)
x2,y2=(事件x+4),(事件y+4)
创建椭圆(x1,y1,x2,y2,fill=“黑色”)
b1=“向下”#您只想在按钮按下时绘制
#因为“运动”事件一直在发生-
def b1up(事件):
全局b1、xold、yold
b1=“向上”
xold=None#松开按钮后重置线路
yold=无
def运动(事件):
如果b1==“向下”:
全局xold
x1,y1=(事件x-4),(事件y-4)
x2,y2=(事件x+4),(事件y+4)
创建椭圆(x1,y1,x2,y2,fill=“黑色”)
如果xold不是None,yold不是None:
python_green=“#476042”
x1,y1=(事件x-4),(事件y-4)
x2,y2=(事件x+4),(事件y+4)
创建椭圆(x1,y1,x2,y2,fill=“黑色”)
创建一行(xold,yold,event.x,event.y,smooth=TRUE,width=9)
#这是你画的地方。光滑的。整洁的
xold=event.x
yold=event.y
如果名称=“\uuuuu main\uuuuuuuu”:
main()
此程序与其他画布绘制程序不同,因为它同时使用线条和椭圆,最大限度地减少了线条中出现间隙的可能性,感谢所有看过此程序的人,我希望您发现此程序很有用只是说,您真的不应该提出异常并使用try-catch来打破loopsok,我会解决它,我没有把它放在那里,这是我从程序中得到的,我只是下载了它:-)