Tkinter Visualization of Python development

Source: Internet
Author: User

Introduction:
The 1.Tkinter module is the interface of Python's standard TK GUI toolkit and is a python built-in module that can be imported directly.
2.Tkinter in Python2 and python3 changes in large, in Python2 called Tkinter, in Python3 called Tkinter.
3. This article is in the python2.7 version test

I. Creating a first window

fromimport *  #导入Tkinter这个模块root=Tk()  #对Tk这个类实例话root.mainloop()  #进行事件循环

Note:
The TK class represents the high-level space of most application main windows, inheriting from two classes (Misc and WM), that is, all parts need to be created first

Two. Components (widgets): size, position, and functionality

1.Label:
Description: Label
Usage: Label(根对象,[属性列表(用字典的形式)])
Example:name=Label(root,name=‘name‘,bg=‘green‘)

2.Button:
Description: Button
Usage: Button(根对象,[属性列表(用字典的形式)])#command属性可以用来绑定按钮点击事件
Example:

def push():    print‘you push me‘btn=Button(root,text=‘push‘,command=push)

3.Frame
Description: Creates a rectangular area, as a container
Usage: Frame(根对象,[属性列表(用字典的形式)])
Example:

frm=frame (Root)  #left  frm_l= Frame (frm) Label (frm_l,text= ' left_top ' ). pack  (Side=top) Label (Frm_l,text= ' Left_bottom ' ). pack  (side=top) Frm_l.pack  (side=left)  #right  Frm_r =frame (frm) Label (frm_r,text= ' right_top ' ). pack  (Side=top) Label (Frm_r,text= ' Right_bottom ' ). pack  (side=top) Frm_r.pack  (side=right) frm. pack  ()

21 Core components in 4.Tkinter
TopLevel, Label, Button, Canvas, Checkbutton, Entry, Frame, Labelframe, Listbox, Menu, Menubutton, Message, Optionmenu, Panewindow, RadioButton, scale, Scrollbar, Spinbox, Text, Bitmap, Image

Three. Layout

1.pack layout
<1> using the pack function, the components are ranked down by default;
Parameters of the <2>pack function:

    • Side: Specifies that the dock is in that direction, the value left right TOP BOTTOM
    • Fill: Specify the fill direction, value x Y BOTH NONE
    • Expand: whether stretched, desirable value Yes NO
    • Anchor: Desirable value n E S W or combination
pack(side=LEFT,expand=NO,fill=X)

2.grid Grid Layout
Grid function Parameters:

    • Row represents rows, and column represents columns
    • Sticky determines the direction in which the component starts, the desired value: N E W S
    • RowSpan represents the number of rows spanned, ColumnSpan represents the number of columns spanned
label.grid(row=1,column=0)

Four. Events
Binding an event using the bind () function

窗体对象.bind(事件类型,回调函数)

Five. dialog boxes and message boxes

1. Message box

fromimport *print showerror(title=‘‘,message=‘‘)#其中,还有其他类型消息框,show...,ask...。

2. dialog box
<1> modal dialog box Simpledialog

from SimpleDialog import *dlg=SimpleDialog(root,text=‘‘,buttons=[‘Yes‘,‘NO‘,...])print dlg.go()#用户点击了那个按钮

<2> using the Tksimpledialog module

fromimport *print askfloat(title=‘‘,prompt=‘‘,minivalue=0,maxvalue=100)#与askfloat()相同的方法还有ask integer、asserting,只不过属性有所不同

<3> file dialog box

fromimport *fileDlg=LoadFileDialog(root)print fd.go()#返回用户选中的文本路径fileDlg=SaveFileDialog(root)#保存文件print fd.go()

Six. Hand-drawn graphics

can=Canvas(root,width=300,height=50)can.create_line((0,0),(200,200),width=4)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Tkinter Visualization of Python development

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.