Simple graphic library for beginners
Christian Gollwitzer
auriocus at gmx.de
Fri Jan 12 03:00:19 EST 2018
Am 11.01.18 um 06:16 schrieb Michael Torrie:
> On 01/10/2018 01:13 PM, bartc wrote:
>> I couldn't see anything obviously simple there. A lot seems to do with
>> interaction which is always much more complicated than just drawing stuff.
>
> Yes the link didn't have the simple examples I hoped for. How's this:
> -----------------------------
> import pygame
> import time
>
> pygame.init()
> screen = pygame.display.set_mode((1024, 768) )
> red = (255,0,0)
> green = (0,255,0)
>
> screen.fill( (255,255,255) )
> pygame.draw.lines(screen, red, False, ((0,0),(100,100)))
> pygame.draw.lines(screen, green, False, ((0,100),(100,0)))
> pygame.display.update()
>
> time.sleep(5)
> pygame.quit()
> ------------------------------
This looks very reasonable. If one wants buttons, however, I still
recommend to use Tkinter. The same program looks not much more complex:
----------------------------------
import tkinter as Tk
from tkinter import ttk
# Boilerplate: set up a resizeable canvas in a window
root = Tk.Tk()
canvas = Tk.Canvas(root)
canvas.pack(expand=True, fill='both')
canvas.create_line(0, 0, 100, 100, fill='red')
canvas.create_line(0, 100, 100, 0, fill='green')
# add a button to close the program
button = ttk.Button(root, text='Leave', command=exit)
button.pack()
# run the program
Tk.mainloop()
-----------------------------------
The boilerplate looks a bit less intuitive - instead of pygame.init()
you have to set up a canvas in a window - but the actual drawing code
looks quite similar. And it is trivial to add a button or other widgets,
as shown above.
Christian
More information about the Python-list
mailing list