Widgets - Tkinter

From TRCCompSci - AQA Computer Science
Revision as of 12:01, 19 September 2018 by 000030906 (talk | contribs) (Frame)
Jump to: navigation, search

Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI application. These controls are commonly called widgets.

Frames And Adding Widgets

Frame

The Frame widget is used to hold other widgets. After you create your root window, the first thing you should do is add a frame.

Imagine the root window as a wall in the classroom. The Frame widget is like a board that we can place on the wall, and then we can place other widgets onto that board. We could place those widgets directly onto the wall (the root window), but this isn't advised as can easily cause us issues.


We can then use a class to define our frame. The class is instantiated after the root window is created, passing the root window as its master. In the example code below, I've called this class for our frame "MainFrame". An instance of MainFrame is created and called "app", after the root window ("root") is created.


Note: The root window is passed to its constructor to act as the frame's master.

Note: Until an instance of the class has been created, the code for the class won't be executed. In this code, the instance of the class is created with "app = MainFrame(root)".


from tkinter import *

class MainFrame(Frame:)
    def__init__(self, master):
       super(MainFrame, self).__init__(master)
       self.grid()

root = Tk()
root.title("My GUI")
root.geometry("400x200")

app = MainFrame(root)

root.mainloop()

LabelFrame

Still WIP

Adding Widgets

Still WIP

Buttons

WIP

Labels

WIP

Entry

WIP

Text

WIP

CheckBox

WIP

RadioButton

WIP

OptionMenu

WIP

Menus

WIP