【问题标题】:python tkinter drawing programpython tkinter 绘图程序
【发布时间】:2016-06-03 08:52:20
【问题描述】:

我有 2 个独立的程序,都是从 Stack Overflow 获得的,并且都可以独立运行。这是第一个:

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

这个程序应该在 tkinter 框架中嵌入一个 pygame 窗口,它就像一个魅力,这是第二个程序:

    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 按钮、条目等。

两个程序单独工作都很好,但是,当我想替换第一个程序中的 pygame 部分时,一切正常,除了我无法绘制任何东西,按钮不想被点击,我不能通过x退出,这根本没有意义,所以我在想可能有问题,反正我找不到问题,所以我很感激任何建议。

【问题讨论】:

  • 只是说,你真的不应该引发异常并使用 try-catch 来打破循环
  • 好的,我把它拿出来,不过我没有把它放在那里,这是我从程序中得到的,我只是下载了它:-)

标签: python user-interface python-3.x tkinter pygame


【解决方案1】:

我自己已经想出了答案,我使用 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()

这个程序与其他画布绘图程序不同,因为它同时使用线条和椭圆,最大限度地减少线条中出现间隙的机会,感谢任何查看此内容的人,我希望你觉得这很有用

【讨论】:

    猜你喜欢
    • 2017-07-19
    • 2016-11-20
    • 1970-01-01
    • 2020-10-09
    • 1970-01-01
    • 2021-03-04
    • 2019-05-15
    • 1970-01-01
    • 2020-05-02
    相关资源
    最近更新 更多