引:Python GUI程式設計(Tkinter)

weixin_33860722發表於2015-12-28

Python提供了開發圖形使用者介面(GUI)的各種方案。下面列出最重要如下:

Tkinter:Tkinter的是Tk的GUI工具包,與Python附帶的Python介面。在本教程中我們將看看這個選項.

wxPython:這是一個開源的Python介面的wxWindowshttp://wxpython.org.

JPython:JPython的是一個用於Java的Python埠,

這使得Python指令碼在本地機器上的無縫接入到Java類庫http://www.jython.org.

有很多可用它,我這裡沒有列出的其他介面。您可以通過網路找到它們.

Tkinter 程式設計:

TkInter是標準的Python GUI庫。的Python與Tkinter的結合提供了一個快速和容易的方法來建立GUI應用程式。 Tkinter的提供了一個強大的物件導向的介面Tk的GUI工具包.

使用Tkinter建立一個GUI應用程式是一件容易的事。所有你需要做的是執行以下步驟:

匯入Tkinter模組.

建立GUI應用程式的主視窗.

新增上述部件之一或更多的GUI應用程式.

進入主事件迴圈的由使用者觸發每個事件響應.

例子:

#!/usr/bin/python

import Tkinter

top = Tkinter.Tk()

# Code to add widgets will go here...

top.mainloop()

這將建立一個下面的視窗:

1361228-ddfb475b289aafdd.jpg

Tkinter的部件:

Tkinter的提供各種控制元件,如按鈕,標籤和文字框,一個GUI應用程式中使用。這些控制元件通常被稱為部件.

目前有15種Tkinter的部件。我們提出這些部件以及一個簡短的介紹,在下面的表:

OperatorDescription

Button  


The Button widget is used to display buttons in your application.

Canvas


The Canvas widget is used to draw shapes, such as lines, ovals, polygons, and rectangles, in your application.

Checkbutton


The Checkbutton widget is used to display a number of options as checkboxes. The user can select multiple options at a time.

Entry


The Entry widget is used to display a single-line text field for accepting values from a user.

Frame


The Frame widget is used as a container widget to organize other widgets.

Label


The Label widget is used to provide a single-line caption for other widgets. It can also contain images.

Listbox


The Listbox widget is used to provide a list of options to a user.

Menubutton


The Menubutton widget is used to display menus in your application.

Menu


The Menu widget is used to provide various commands to a user. These commands are contained inside Menubutton.

Message


The Message widget is used to display multiline text fields for accepting values from a user.

Radiobutton


The Radiobutton widget is used to display a number of options as radio buttons. The user can select only one option at a time.

Scale


The Scale widget is used to provide a slider widget.

Scrollbar


The Scrollbar widget is used to add scrolling capability to various widgets, such as list boxes.

Text


The Text widget is used to display text in multiple lines.

Toplevel


The Toplevel widget is used to provide a separate window container.

Spinbox


The Spinbox widget is a variant of the standard Tkinter Entry widget, which can be used to select from a fixed number of values.

PanedWindow


A PanedWindow is a container widget that may contain any number of panes, arranged horizontally or vertically.

LabelFrame


A labelframe is a simple container widget. Its primary purpose is to act as a spacer or container for complex window layouts.

tkMessageBox


This module is used to display message boxes in your applications.

標準屬性:

讓我們來看看如何他們的一些共同的屬性。如大小,顏色和字型指定.

Dimensions

Colors

Fonts

Anchors

Relief styles

Bitmaps

Cursors

幾何管理:

Tkinter的部件有特定幾何形狀的管理方法,整個小部件父控制元件區域組織的目的。 Tkinter的公開以下幾何經理類:包裝,網格,位置.

pack()方法- 這個的幾何管理器組織之前,將其放置在他們的父widget塊部件.

grid()方法- 這的幾何管理器組織表狀結構中的小部件的父部件.

place()方法-這個的幾何管理器組織放置在一個特定的位置,在他們的父widget部件.

相關文章