Notes
Before using simplegui, you have to first import simplegui module.
import simplegui
Definitions:
An event is a signal that informs an application that something important has occurred.
Event handlers are procedures that are called when a corresponding event occurs.
Timer
import simplegui
#Timer Event Handler
def timer_handler():
...
#Create a Timer
timer_name = simplegui.create_timer(interval, timer_handler)
#Start Timer
timer_name.start()
Frame
import simplegui
#Create a Frame
frame_name = simplegui.create_frame(title, canvas_width, canvas_height [, control_width])
#Start Frame's Interactivity
frame_name.start()
Adding Control Objects in the Frame
import simplegui
#Create a Frame
frame_name = simplegui.create_frame(title, canvas_width, canvas_height [, control_width])
#Control objects are placed in the control panel and are placed in the top-down order.
#Create a Label
object_name = frame_name.add_label(text [, width]) #By default, if the width parameter is omitted, it will fit the given text of the object.
#Create a Button
object_name1 = frame_name.add_button(text, button_handler [, width]) #button_handler is triggered when the button is clicked, button_handler does not take any parameter
#Create a Text Input Box
object_name2 = frame_name.add_input(text, input_handler, width) #input_handler is triggered when the enter key is pressed. input_handler takes 1 parameter which is the text in the input box.
#Start Frame's Interactivity
frame_name.start()
Get or Set the Text of Control Objects
import simplegui
#Create a Frame
frame_name = simplegui.create_frame(title, canvas_width, canvas_height [, control_width])
#Create a Text Input Box
object_name = frame_name.add_input(text, input_handler [, width])
#Print the text in the Text Input Box
print(object_name.get_text())
#Set the text in the Text Input Box
object_name.set_text(text)
#Start Frame's Interactivity
frame_name.start()
Adding Keyboard and Mouse Handlers for a Frame
import simplegui
#Create a Frame
frame_name = simplegui.create_frame(title, canvas_width, canvas_height [, control_width])
#Adding Keydown Handler to the Frame
frame_name.set_keydown_handler(key_down_handler) #key_down_handler is triggered once when any key is pressed. key_down_handler takes 1 parameter which is the key code of the key.
#Adding Keyup Handler to the Frame
frame_name.set_keyup_handler(key_up_handler) #key_up_handler is triggered once when any key is released. key_up_handler takes 1 parameter which is the text in the key code of the key.
#Adding Mouse Click Handler to the Frame
frame_name.set_mouseclick_handler(mouse_click_handler) #mouse_click_handler is triggered when a mouse button is clicked on the canvas. mouse_click_handler takes 1 parameter which is the pair of the mouse coordinate. (The pair is a tuple of two non-negative integers such as (30, 50))
#Adding Mouse Drag Handler to the Frame
frame_name.set_mousedrag_handler(mouse_drag_handler) #mouse_drag_handler is triggered for every new position when a mouse button is pressed and moved on the canvas. mouse_drag_handler takes 1 parameter which is the pair of the mouse coordinate. (The pair is a tuple of two non-negative integers such as (30, 50))
#Start Frame's Interactivity
frame_name.start()
Add Draw Handler on Canvas
import simplegui
def draw_handler(canvas):
…
#Create a Frame
frame_name = simplegui.create_frame(title, canvas_width, canvas_height [, control_width])
#Adding Draw Handler to the Canvas
frame_name .set_draw_handler(draw_handler) #Trigger the draw_handler event and send the canvas object; draw_handler takes 1 parameter which is the canvas object
#Start Frame's Interactivity
frame_name.start()
Drawing on a Canvas
import simplegui
def draw_handler(canvas):
#Print Text
canvas.draw_text(text, position, size, color [, font_face]) #The position parameter is a 2-element tuple or list.
#Draw a Line
canvas.draw_line(start_point, end_point, width, color) #The start_point and end_point parameters are 2-element tuples or lists.
#Draw a Sequence of Connected Line Segments
canvas.draw_polyline(point_list, width, color) #The point_list is a list of 2-element tuples or lists (ex. [(10,0), (20,50),(50,20)]).
#Draw a Polygon
canvas.draw_polygon(point_list, width, color, [fill_color]) #The point_list is a list of 2-element tuples or lists (ex. [(10,0), (20,50),(50,20)]). The last point will be connected to the first point.
#Draw a Circle
canvas.draw_circle(center, radius, width, color, [fill_color]) #The center parameter is a 2-element tuple or list.
#Draw a Point
canvas.draw_circle(position, color) #The position parameter is a 2-element tuple or list.
#Draw a Image
image_name = simplegui.load_image(image_URL) #Load a image from a given URL of the image
canvas.draw_image(image_name, center_source, dimensions_source, center, dimensions [, rotation]) #The center_source parameter is a 2-element tuple or list represents the center of the original image. The dimensions_source is a 2-element tuple or list represents the width and height of the original image. The center parameter is a 2-element tuple or list represents the center of the final image on the canvas. The dimensions is a 2-element tuple or list represents the width and height of the final image on the canvas.
#Create a Frame
frame_name = simplegui.create_frame(title, canvas_width, canvas_height [, control_width])
#Adding Draw Handler to the Canvas
frame.set_draw_handler(draw_handler) #Trigger the draw_handler event and send the canvas object; draw_handler takes 1 parameter which is the canvas object
#Start Frame's Interactivity
frame.start()
Sound
import simplegui
#Load sound file
sound_name = simplegui.load_sound(sound_URL)
#Set Volume
sound_name.set_volume(volume) #volume can be any number from 0(silent) to 1.0(maximum) scale. Default is 1
#Control
sound_name.play() #Play sound
sound_name.pause() #Pause sound
sound_name.rewind() #Stop the sound, and then rewind to the beginning.