-
Notifications
You must be signed in to change notification settings - Fork 1
Window: A Graphics Window
The Window is what you use to draw objects on top of and internally, it is an implementation of a Tkinter TopLevel Canvas. It creates a window that you can customize a lot of options with but before we get to that, let us take a look at the basics. Here is how you create a window:
from goopylib.Window import Window # importing only the Window
window = Window(title="This is a window!", width=500, height=500)
while True: # This acts as the mainloop
window.update_win() # Update the window
This should output a white window with a width & height of 500 pixels. To draw on objects on the Window , we use the draw
function which is implemented in every GraphicsObject.
# Place this code before the while loop if you want to draw the Rectangle at the start
# Make sure you import the Rectangle & Point classes
rect = Rectangle(Point(100, 100), Point(400, 400)).draw(window) # Here, we pass the variable which references the Window
Now, let's try creating a program that detects if the rectangle has been clicked:
# You can edit the mainloop to this
while True:
mouse_pos = window.check_left_mouse_click() # Getting the coordinates of a click if one occured
if rect.is_clicked(mouse_pos):
print("Rectangle was Clicked")
mouse_pos = window.check_mouse_motion() # Gets current mouse position
if rect.is_clicked(mouse_pos):
print("Mouse hovering over Rectangle")
The Window has several customizable options which are listed here. You can set these functions while creating the Window like the example shown with the title, width, and height.
-
title
- the title of the window (string
)
The window dimensions & position are all rounded down to the nearest integer
-
width
,height
- width & height of the window in pixels (int or float) -
min_width
,min_height
- if the window is resizable, the minimum dimensions it can have (int or float) -
max_width
,max_height
- if the window is resizable, the maximum dimensions it can have (int or float) -
resizable_width
,resizable_height
- is the window resizable? (boolean
) -
x_pos
,y_pos
- position of the window relative to the top-left of your screen (int or float) -
style
- if a style is assigned, the window attributes will be configured from it. Look at [styles] (To-Do) for more -
bk_colour
- background colour of the window, overrides the style (Colour
type, see Colours in Goopy) -
icon
- a .ico file that shows up as the window icon (string
, path to the file)
It is recommended that autoflush
is False
and that you update the window manually as doing otherwise can cause blinking objects when redrawing and other issues.
-
autoflush
- does the window update automatically? (boolean
) -
cursor
- what should the mouse cursor be inside the window? (string
, see Cursors in Goopy) -
border_relief
- the relief of the window border (string
, see [Relief List] (To-Do)) -
border_width
- width of the border in pixels (integer
orfloat
)
This is a list of all the functions in the Window.
-
close()
- Closes the window -
update_win()
- Updates the window -
is_closed()
- Checks if the window is closed -
is_open()
- Checks if the window is open -
clear()
- Clears the window -
redraw()
- Redraws everything on the window
These functions allow you to take input from the user and are in the format
check_{mousebutton}_mouse_{type}()
# and
get_{mousebutton}_mouse_{type}()
This is the general format for mouse event functions and they each return a Point object Point(mouse_x, mouse_y)
with the coordinates of the event. mousebutton
can be anyone of left
, right
, and middle
while {type}
can be either click
or press
.
To check/get double or triple mouse events, the format changes to check_{clicks}_{mousebutton}_mouse_{type}()
and get_{clicks}_{mousebutton}_mouse_{type}()
where {clicks}
can be either double
or triple
.
For example: check_left_mouse_click()
, get_middle_mouse_press()
, etc.
The difference between the get and check variants is that getting waits until an event occurs whereas check returns a None
value if no event has occurred.
There are a few other functions too
-
check_mouse_scroll()
- Gets the value of the mouse scroll wheel -
get_last_mouse()
- Gets the location of the last event -
check_mouse_motion()
- Gets the location of the mouse if it has moved -
check_left_mouse_down()
-left
can be replaced with eithermiddle
orright
. This returnsTrue
if the mouse button is being held andFalse
otherwise.
The Keyboard functions do not currently work and so they are not included here right now.
The dx
, dy
, x
, y
, & time
values are all integers or floats (internally rounded down except for time
). All of these functions return self
, i.e. the window object itself.
-
move(dx, dy)
- Moves the window by dx, dy -
move_x(dx)
- Moves the window by dx -
move_y(dy)
- Moves the window by dy -
move_to(x, y)
- Moves the window to the specified location -
move_to_x(x)
- Moves the window to the X location -
move_to_y(y)
- Moves the window to the Y location -
move_to_point(p)
- Moves the window to the Point specified asPoint(x, y)
Gliding functions move the window to a location over a period of time rather than just instantly. The time
& easing
arguments for these functions need not be provided as they default to 1
and ease_linear()
respectively.
For more information about easing functions, see [Easing in Goopy] (To-Do). These functions return the window object too.
-
glide(dx, dy, time, easing_x, easing_y)
- Moves the window by dx, dy -
glide_x(dx, time, easing)
- Moves the window by dx -
glide_y(dy, time, easing)
- Moves the window by dy -
glide_to(x, y, time, easing_x, easing_y)
- Moves the window to the specified location -
glide_to_x(x, time, easing)
- Moves the window to the X location -
glide_to_y(y, time, easing)
- Moves the window to the Y location -
glide_to_point(p, time, easing_x, easing_y)
- Moves the window to the Point specified asPoint(x, y)
-
get_window_pos()
- Returns the position of the window relative to the top-left of the screen as aPoint(x, y)
object -
get_bk_colour()
- Returns the background colour of the window as aColour
object -
get_size()
,get_width()
,get_height()
- Return the size (width, height
) of the window in pixels -
get_min_size()
,get_min_width()
,get_min_height()
-
get_max_size()
,get_max_width()
,get_max_height()
-
is_resizable()
,is_width_resizable()
,is_height_resizable()
- Return aboolean
value -
get_border_width()
,get_border_relief()
get_cursor()
get_title()
-
get_coords
- Returns the window coordinatesPoint(topleft), Point(bottomright)
These are all pretty self-explanatory and the inputs arguments are the same as those in the options.
set_background(colour)
set_border_width(width)
set_border_relief(relief)
set_cursor(cursor)
These resizable values are True
by default
set_resizable(resizable_width, resizable_height)
set_resizable_width(resizable_width)
set_resizable_height(resizable_height)
set_icon(icon)
set_title(title)
set_width(width)
set_height(height)
set_min_height(min_height)
set_min_width(min_width)
set_size(width, height)
set_min_size(min_width, min_height)
set_max_size(max_width, max_height)
set_max_width(max_width)
set_max_height(max_height)
-
set_coords(x1, y1, x2, y2)
- This sets the virtual coordinates of the window from top-left (x1, y1
) to bottom-right (x2-y2
) without affecting the actual coordinates. This can be used to move the view of the window around, make this value a constant, etc. You can also stretch the window through this though it is not recommended astext
&entries
won't undergo the stretch.
-
plot(x, y, colour)
- Plots a pixel atx, y
with a colour (defaults toBLACK
) -
flush()
- Updates the drawing on the window (not recommended, useupdate_win()
instead) -
update()
- Updates the window (not recommended, useupdate_win()
instead) -
save_canvas()
- Saves a snapshot of the window (doesn't work properly yet)
These functions undraw specific/all the graphics objects present on the window.
undraw_all()
-
undraw_all_instances(obj)
- Undraws all objects which are the same type as that ofobj
undraw_all_lines()
undraw_all_circles()
undraw_all_rectangles()
undraw_all_ovals()
undraw_all_images()
These functions destroy specific/all objects on the window.
destroy_all()
destroy_all_instances(obj)
destroy_all_lines()
destroy_all_circles()
destroy_all_rectangles()
destroy_all_ovals()
destroy_all_images()
It is recommended that the autoflush
parameter of the Window is False
and we can demonstrate why using a simple example:
from goopylib.imports import *
window = Window(title="This is a window!", width=500, height=500) # Autoflush defaults to True
rect = Rectangle(Point(10, 230), Point(50, 270)).draw(window)
rect.glide(dx=200, time=2) # This makes the Rectangle slowly move across the screen 200 pixels in 2 seconds
while True:
update(24)
Now compare this code to
from goopylib.imports import *
window = Window(title="This is a window!", width=500, height=500, autoflush=False)
rect = Rectangle(Point(10, 230), Point(50, 270)).draw(window)
rect.glide_x(dx=200, time=2) # This makes the Rectangle slowly move across the screen horizontally (x) 200 pixels in 2 seconds
while True:
window.update_win() # Updating the window manually
update(24)
You will notice that in the first example, the Rectangle will tend to well.. not move unless you move your mouse over the window, click it, or something else that triggers the window updating. This is in stark contrast to the second example where the motion of the rectangle is very fluid.
Sometimes, when drawing objects and moving them manually (not through the glide
function but the move
function) the objects will tend to flicker if autoflush is off because the window is updating both when the object undraws, and also when it draws itself again.
Feel free to use this library in any of your projects and let me know about them!
Contact Author: bhavyemathur@gmail.com