How to Make a Window in Python

104 17
    • 1). Setup the program up for Tkinter. In the text editor, enter the following code:

      #!/usr/bin/python

      from Tkinter import *

      root = Tk()

      This sample code imports the Tkinter libraries into the working program. This means that the programmer can access the Tkinter libraries programatically through Tkinter objects. Then, the variable "root" is instantiated as a Tkinter object . This is a root widget, and there should only be one per program.

    • 2). Add the following code in the text editor:

      root.title("New Window")

      root.geometry("200x200")

      root.mainloop()

      "root.title" sets the title of the window (located in the title bar) to "New Window." "Root.geometry" sets the dimensions of the window to 200 pixels by 200 pixels. The "root.mainloop" method is the main method: the window will not appear until the main loop executes.

    • 3). Save the file and execute the program:

      python filename.py

      Where "filename" is the name of the save file. The window should appear, as a 200x200 square with the proper title.

Subscribe to our newsletter
Sign up here to get the latest news, updates and special offers delivered directly to your inbox.
You can unsubscribe at any time

Leave A Reply

Your email address will not be published.

"Home & Garden" MOST POPULAR

How to Restore Vinyl Dashboards

How Do I Trap Smoke Odor?

Homemade Non-Toxic Cleaners

How to Treat Wet Carpet

How to Clean Plastic Bathtubs

Black Algae Turf Mold

How to Cover Bleach on Carpets