python tkinter绘图程序

时间:2016-06-03 08:52:20

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

我有2个独立的程序,我都是从stackoverflow获得的,而且这两个程序都是自己的第一个:

    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屏幕,你可以画任何你喜欢的东西。

我不是一个非常expgnced pygame程序员但是,我有使用tkinter的expierence,我想做的是制作一个你可以绘制的程序,但它也必须有tkinter按钮,条目等。

两个程序单独工作完美,但是,当我想在第一个程序中替换pygame部分时,一切正常,除了,我不能画任何东西,按钮不想被点击,我不能通过x退出,这根本没有意义,所以我想也许有些不对劲,无论如何,我无法找到问题,所以对于任何建议都是如此。

谢谢

1 个答案:

答案 0 :(得分:0)

我实际上想出了自己的答案,我使用pygame的原因是因为我没想到你可以使用tkinter,但我制作了一个程序,使用tkinter并且actuely画得相当不错(也许比pygame更好)我会添加这个程序

var gg = NSDictionary()
    var siteName = NSString()

    override func viewDidLoad() {
        super.viewDidLoad()

        siteName = gg["SiteName"]! as! NSString
        print(siteName)

        self.siteNameDetail.text = siteName as String
    }

这个程序与其他画布绘图程序不同,因为它使用了线条和椭圆形,最大限度地减少了线条间隙的可能性,感谢任何观看此内容的人,我希望你发现这个有用

相关问题