Python Tkinter Hello,Tkinter__Python

來源:互聯網
上載者:User
Hello, Tkinter

But enough talk. Time to look at some code instead.

As you know, every serious tutorial should start with a “hello world”-type example. In this overview, we’ll show you not only one such example, but two.

每一個嚴肅的教程都會以一個“hello world”類型的例子開頭,為了保持這個慣例我們要為你展示兩個這樣的例子。

First, let’s look at a pretty minimal version:

首先,讓我們看一個漂亮的最小版本: Our First 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:

運行這個指令碼:

$ python hello1.py

The following window appears.

會出現下面的視窗。 Running the program

To stop the program, just close the window.

想要停止這個視窗,關閉它就可以了。 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, you can simply import everything from Tkinter into your module’s namespace:

它包含所有的類、函數和Tk套件需要的物件。大多數情況下,可以簡單的匯入Tkinter的一切到模組的命名空間。

from Tkinter import *

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

為了去初始化Tkinter,需要建立一個Tk 根組件,這是一個由視窗管理器提供的原生視窗,包含一個標題列和其他裝飾。每一個程式只應該有一個根組件,它必須在其他組件之前建立。

root = Tk()

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

然後,建立一個Label組件作為根視窗的子組件。

w = label(root, text = "hello, world")w.pack()

A Label widget can display either text or an icon or other image. In this case, we use the text option to specify which text to display.

Label組件可以展示任意文本或者表徵圖或者映像。在這個例子裡,使用了text選項去指定要顯示的文本。

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

最後,在這個組件上調用pack方法。這個方法會根據給出的文本自動化佈建組件的大小並變得可視。總之,在進入Tkinter事情迴圈之前視窗不會出現。

root.mainloop()

The program will 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 (such as redraw events and window configuration messages), it also handle operations queued by Tkinter itself. Among these operations are geometry management (queued by the pack method) and display updates. This also means that the application window will not appear before you enter the main loop.

在關閉 應用視窗之前程式都會等等事件。事件迴圈不單單操作來自使用者(像滑鼠點擊和按鍵)或者視窗系統的事件,它同樣操作來自於Tkinter自身的事件。在這些操作內是幾何管理(由pack方法排列)和顯示更新。這也意味著在進入main迴圈前應用視窗不會出現。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.