Difference between revisions of "Widgets - Tkinter"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Frame)
(LabelFrame)
Line 36: Line 36:
  
 
===LabelFrame===
 
===LabelFrame===
Still WIP
+
 
 +
The LabelFrame widget is a variation of the Frame widget. It works in a similar way, but gives you the option to add a title and border around your widgets, allowing you to improve and organise your layout.
 +
 
 +
<syntaxhighlight lang=python>
 +
from tkinter import *
 +
 
 +
class MainFrame(Frame:)
 +
    def __init__(self, master):
 +
        super(MainFrame, self).__init__(master)
 +
        self.grid()
 +
        self.create_widgets()
 +
 
 +
    def create_widgets(self):
 +
        self.frame1 = LabelFrame(self, text = "Frame1")
 +
        self.frame1.grid(row = 0, column = 0)
 +
 
 +
        # placing a label inside the labelframe so we can see the effect
 +
        self.lbl1 = Label(self.frame1, text = "This is Label 1")
 +
        self.lbl1.grid(row = 0, column = 0)
 +
 
 +
root = Tk()
 +
root.title("My GUI")
 +
root.geometry("400x200")
 +
 
 +
app = MainFrame(root)
 +
 
 +
root.mainloop()
 +
</syntaxhighlight>
  
 
===Adding Widgets===
 
===Adding Widgets===

Revision as of 13:33, 19 September 2018

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

The LabelFrame widget is a variation of the Frame widget. It works in a similar way, but gives you the option to add a title and border around your widgets, allowing you to improve and organise your layout.

from tkinter import *

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

    def create_widgets(self):
        self.frame1 = LabelFrame(self, text = "Frame1")
        self.frame1.grid(row = 0, column = 0)

        # placing a label inside the labelframe so we can see the effect
        self.lbl1 = Label(self.frame1, text = "This is Label 1")
        self.lbl1.grid(row = 0, column = 0)

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

app = MainFrame(root)

root.mainloop()

Adding Widgets

Still WIP

Buttons

WIP

Labels

WIP

Entry

WIP

Text

WIP

CheckBox

WIP

RadioButton

WIP

OptionMenu

WIP

Menus

WIP