Creating a Window - Tkinter

From TRCCompSci - AQA Computer Science
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.