STUDENT REQUESTED TUTORIAL (TU11)
OPTIONAL
Not required for IGCSE, AS or A2
This tutorial is purely for you to enjoy and learn how to make applications for Windows. It is about AS level difficulty wise.
Not required for IGCSE, AS or A2
This tutorial is purely for you to enjoy and learn how to make applications for Windows. It is about AS level difficulty wise.
starting up tkinter
import tkinter # imports main library
import random # For random functions
window = tkinter.Tk() #so that there's less confusing brackets
The first three lines bring in the commands we need to get tkinter started.
def RandomNumber():
MyRandom = random.randint(1,6)
dice_thrown.configure(text="Dice thrown: " + str(MyRandom))
Now that we have started programming in Windows, we need to set up functions for when buttons are clicked or commands need to be executed. This function, makes a random number and then changes text.
Note text="Dice thrown: " + str(MyRandom) must be a string and cannot include numbers.
MyTitle = tkinter.Label(window, text="Random Number Generator",font="Helvetica 16 bold")
MyTitle.pack()
This is the first label. It is a simple text label with the font Helvetica, font size 16 in bold. You can change it to any font on your system. The pack command tells tkinter to organise the window.
MyButton = tkinter.Button(window, text="OK", command=RandomNumber) MyButton.pack()
Same again, but a button. Note the command This will run a function when pressed.
dice_thrown = tkinter.Label(window, font="Helvetica 16 bold")
dice_thrown.pack()
When the program starts there is no text, but we need to pack it anyway, so that tkinter leaves space in the window for it.
Finally finish with the Windows loop.
window.mainloop()
import random # For random functions
window = tkinter.Tk() #so that there's less confusing brackets
The first three lines bring in the commands we need to get tkinter started.
def RandomNumber():
MyRandom = random.randint(1,6)
dice_thrown.configure(text="Dice thrown: " + str(MyRandom))
Now that we have started programming in Windows, we need to set up functions for when buttons are clicked or commands need to be executed. This function, makes a random number and then changes text.
Note text="Dice thrown: " + str(MyRandom) must be a string and cannot include numbers.
MyTitle = tkinter.Label(window, text="Random Number Generator",font="Helvetica 16 bold")
MyTitle.pack()
This is the first label. It is a simple text label with the font Helvetica, font size 16 in bold. You can change it to any font on your system. The pack command tells tkinter to organise the window.
MyButton = tkinter.Button(window, text="OK", command=RandomNumber) MyButton.pack()
Same again, but a button. Note the command This will run a function when pressed.
dice_thrown = tkinter.Label(window, font="Helvetica 16 bold")
dice_thrown.pack()
When the program starts there is no text, but we need to pack it anyway, so that tkinter leaves space in the window for it.
Finally finish with the Windows loop.
window.mainloop()
Exercise 1: Instead of throwing dice, get it to come up with a random name picker!