Creating a Window - Tkinter

From TRCCompSci - AQA Computer Science
Revision as of 10:48, 14 September 2018 by 000030906 (talk | contribs) (000030906 moved page Creating a Window - Python to Creating a Window - Tkinter: changing the way i name these pages)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Creating the basic window for your application is much simpler with Python and Tkinter than in most other languages. We start by creating an instance of the Tk() class from the tkinter library. This creates a 'Top Level' widget, which will act as the main window for our application. Here's an example of the general setup for the main window:

from tkinter import *

root = Tk()
root.title("Window's Title")
root.geometry("400x300")

root.mainloop()

In this code, we import the Tkinter library, we create an instance of the Tk() class (and call it root), we then set the title of the window, as well as set the dimensions of the window, and then finally we call the mainloop() method to open the window and start the loop of events.

Note: The size of the window (geometry) is measured in pixels.