Presentation is loading. Please wait.

Presentation is loading. Please wait.

Noadswood Science, 2014.  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015.

Similar presentations


Presentation on theme: "Noadswood Science, 2014.  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015."— Presentation transcript:

1 Noadswood Science, 2014

2  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015

3 Windows  The majority of programs have windows and buttons that can be used to control them – these make up the graphical user interface (GUI)  Python can make a GUI using the Tkinter module from tkinter import * window Tk() input()  The above code imports Tkinter from Python’s standard library and then creates a tkinter window  input() holds the window open

4 Buttons  The GUI can be made more interactive by adding buttons – a different message will be displayed when the user clicks each button… #Windows from tkinter import * def bAaction(): print("Thanks") def bBaction(): print("Ouch!") window = Tk() buttonA = Button(window, text="Press me!", command=bAaction) buttonB = Button(window, text="Don't press!", command=bBaction) buttonA.pack() buttonB.pack()input() Message to appear when button A is pressed Code to inform Tkinter to put the button in the window Label for button B Tells Tkinter which function to run when the button is clicked

5 Buttons  The buttons make the GUI much easier to use (it is important to keep the GUI simple and clear labelling each button with a sensible name to make the application easy to understand)

6 Dice Roll  Tkinter can be used to build a GUI for a simple program – in this exam the rolling of a six-sided dice…  The program creates a button that, when pressed, runs a function “roll” to display a random number between 1 and 6

7 Dice Roll #Dice from tkinter import * from random import randint def roll(): text.delete(0.0, END) text.insert(END, str(randint(1,6))) window = Tk() text = Text(window, width=3, height=2) buttonA = Button(window, text="Press to roll", command=roll) text.pack() buttonA.pack() input() Imports random number function Creates the text box with specified sizes Text box and button placed within the window Label on button Text inside the text box is cleared and replaced with a random number between 1 and 6 Informs program which function to run when button is pressed

8 Colours  Pictures and graphics on a computer screen are made up of tiny coloured dots called pixels – to create graphics in a program the computer needs to be told exactly what colour each pixel should be  It is important to describe colours in a way that computers can understand – Tkinter includes a useful tool to help with this #Colour Selection from tkinter import * from tkinter.colorchooser import * t = Tk() color = askcolor() print(color)

9 Colours  The colour chooser window will appear and allow the user to choose a colour  The values of the colour are then shown in the shell window – the numbers refer to the red, green and blue which have been mixed to make the chosen colour (followed by a hexadecimal code)… ((186.7265625, 233.91015625, 88.34375), '#bae958')  Pixels can give out red, green or blue light (the combinations of which can make any colour…)

10 Python Colours  Python has a wide range of colours to choose from (either with co-ordinates or via their specific names)…  Python’s Supported Colours can be accessed here...

11 Canvas Drawing  Graphics can be created using Python – firstly a blank area needs to be made which the drawing can go on (known as a canvas)  x and y co-ordinates tell Python exactly where to draw on the canvas  Tkinter x co-ordinates get larger moving to the right and the y co- ordinates get larger moving downwards. (0.0) is in the top-left corner x co-ordinates y co-ordinates (0,0) (300,50) (350,150) (50,200) x co-ordinate is displayed first and the y co-ordinate second

12 Canvas #Canvas from random import * from tkinter import * size = 300 window = Tk() canvas = Canvas(window, width=size, height=size) canvas.pack() circles = 0 while circles <200: col = choice(["DarkTurquoise", "RoyalBlue", "Fuchsia", "Yellow", "Lime", "IndianRed"]) x0 = randint(0, size) y0 = randint(0, size) d = randint(0, size/5) canvas.create_oval(x0, y0, x0 + d, y0 + d, fill=col) circles = circles + 1 window.update() input() Import Tkinter, randint and choice functions Canvas size dimensions Canvas created inside a window Variable set to 0 and loop runs if <200 circles drawn Random choice taken from a selection of colours Circle of random size in a random place on the canvas Circle drawn and counts as +1 Fill with the random colour chosen

13 Canvas

14 Canvas Forever #Canvas Forever from random import * from tkinter import * size = 750 window = Tk() canvas = Canvas(window, width=size, height=size) canvas.pack() while True: col = choice(["DarkTurquoise", "RoyalBlue", "Fuchsia", "Yellow", "Lime", "IndianRed", "Bisque"]) x0 = randint(0, size) y0 = randint(0, size) d = randint(0, size/5) canvas.create_oval(x0, y0, x0 + d, y0 + d, fill=col) window.update() Forever loop draws endless circles


Download ppt "Noadswood Science, 2014.  To know how to use Python to produce windows and colours along with specified co-ordinates Sunday, April 12, 2015."

Similar presentations


Ads by Google