Python Tkinter Hello,tkinter__python

Source: Internet
Author: User
Hello, Tkinter.

But enough talk. Some code instead.

As you know, the every serious tutorial should start with a "Hello World"-type example. In this overview, we'll show you the only one such example, but two.

Every serious tutorial starts with a "Hello World" example, and in order to keep this routine we will show you two examples.

The a pretty minimal version:

First of all, let's look at a pretty minimum version: Our primary Tkinter program (File:hello1.py)

From Tkinter import *

root = Tk ()

w = Label (Root, text= "Hello, world!")
W.pack ()

Root.mainloop ()
Running the Example

To run the program, run the script as usual:

Run this script:

$ python hello1.py

The following window appears.

The following window will appear. Running The program

To stop the program, just close the window.

To stop this window, close it. Details

We start by importing the Tkinter module. It contains all classes, functions and other things needed to work with the TK Toolkit. In most cases, your can simply import everything from tkinter to your module ' s namespace:

It contains all the classes, functions, and objects required by the TK suite. In most cases, you can simply import everything from Tkinter to the namespace of the module.

From Tkinter Import *

To initialize Tkinter, we are have to create a Tk root widget. This is the ordinary window with a title bar and the other decoration provided by your window manager. You are should only the create one root widget for each program, and it must is created before any other widgets.

To initialize Tkinter, you need to create a TK root widget, a native window provided by the window manager that contains a title bar and other decorations. Each program should have only one root component, which must be created before other components.

root = Tk ()

Next, we create a Label widget as a child to the root window:

Then, create a label component as a subassembly of the root window.

w = label (root, Text = "Hello, world")
W.pack ()

A Label widget can display either text or A icon or other image. In the ' case ' we use the ' text option to ' Specify which text to display.

The label component can display any text or an icon or an image. In this example, you use the text option to specify the text to display.

Next, we call the " Pack method" on this widget. This is tells it to size itself the given text, and make itself visible. However, the window won ' t appear until we ' ve entered the Tkinter event loop:

Finally, the pack method is invoked on this component. This method automatically sets the size of the component and becomes visible based on the text given. In short, the window does not appear until you enter the Tkinter affair cycle.

Root.mainloop ()

The program would stay in the event loop until we close the window. The event loop doesn ' t only handle events from the user (such as mouse clicks and key presses) or the windowing system (SU CH as redraw events and Windows configuration messages), it also handle operations by queued Tkinter. Among these operations are geometry management (queued through the pack method) and display updates. This is also means that the application window would not be appear before you enter the main loop.

The program will wait for events before closing the application window. The event loop does not only manipulate events from the user (like mouse clicks and keystrokes) or the window system, but it also operates from events that Tkinter itself. Within these operations are geometry management (arranged by the pack method) and display updates. This also means that the application window does not appear before entering the main loop.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.