90 likes | 227 Views
Graphical User Interfaces. Making the thing look classy!!. Learning Objectives. Understand the need for a GUI Know how to design a GUI Understand how to code a GUI in Python. Why do we need a GUI. The other option is Command Line or punch cards!!!. Nice and Easy to use.
E N D
Graphical User Interfaces Making the thing look classy!!
Learning Objectives • Understand the need for a GUI • Know how to design a GUI • Understand how to code a GUI in Python
Why do we need a GUI • The other option is Command Line or punch cards!!! Nice and Easy to use Annoying and a waste of time Slightly more difficult to use
How does a GUI work in Python Its all about layers and grids Widgets Username Submit Window Frame
Sample Code from tkinter import * root = Tk() root.title("Test") root.geometry("300x300”) root.mainloop() Import the GUI library Create an instance of a window Assign a title to the window Create the size of the window Make the window show The output from the code above
Adding some text to the GUI from tkinter import * root = Tk() root.title("Test") root.geometry("300x300") label = Label(root,text="Username") label.pack() root.mainloop() In python to add text you need to use the Label. The label then needs to be added to the window using pack Output from the code
Adding a button to the GUI from tkinter import * defaddText(): label1 = Label(root, text="You've clicked the button") label1.pack() root = Tk() root.title("Test") root.geometry("300x300") label = Label(root,text="Username") label.pack() btn = Button(root, text="Click Me", command=addText) btn.pack() root.mainloop() In python to add text you need to use the Button. Once again the button is then added to the window using pack To get the button to do something you have to use the command parameter Output from the code Before Click After Click
Adding a text box to the GUI from tkinter import * root = Tk() root.title("Test") root.geometry("300x300") label = Label(root,text="Username") label.pack() txt = Entry(root, bd=1) txt.pack() btn = Button(root, text="Click Me") btn.pack() root.mainloop() In python to add text you need to use the Entry. Once again the button is then added to the window using pack Output from the code