Difference between revisions of "Widgets - Tkinter"

From TRCCompSci - AQA Computer Science
Jump to: navigation, search
(Created page with "Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI application. These controls are commonly called widgets. ==Frame== WIP ===Frame=== St...")
 
(Frame)
Line 1: Line 1:
 
Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI application. These controls are commonly called widgets.
 
Tkinter provides various controls, such as buttons, labels and text boxes used in a GUI application. These controls are commonly called widgets.
  
==Frame==
+
==Frames And Adding Widgets==
WIP
 
  
 
===Frame===
 
===Frame===
Still WIP
+
 
 +
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 eaily 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, tthe instance of class is created with "app = MainFrame(root)".''
 +
 
 +
<syntaxhighlight lang=python>
 +
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()
 +
</syntaxhighlight>
  
 
===LabelFrame===
 
===LabelFrame===

Revision as of 12:00, 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 eaily 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, tthe instance of 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