Python-tkinter

tkinter

tkinter 是 Python 的标准 GUI 库。Python 使用 tkinter 可以快速的创建 GUI 应用程序。由于 tkinter 是内置到 python 的安装包中、只要安装好 Python 之后就能 import tkinter 库、而且 IDLE 也是用 tkinter 编写而成、对于简单的图形界面 tkinter 还是能应付自如。

PS:

The package Tkinter has been renamed to tkinter in Python 3, as well as other modules related to it. Here are the name changes:

Tkinter → tkinter
tkMessageBox → tkinter.messagebox:用于显示在应用程序的消息框
tkColorChooser → tkinter.colorchooser
tkFileDialog → tkinter.filedialog:弹出文件选择框
tkCommonDialog → tkinter.commondialog
tkSimpleDialog → tkinter.simpledialog
tkFont → tkinter.font
Tkdnd → tkinter.dnd
ScrolledText → tkinter.scrolledtext
Tix → tkinter.tix
ttk → tkinter.ttk

常用控件:

说明:

1、Button

t = Button(master, option=value, ...)

master:按钮的父容器

option:可选,该按钮的可设置属性。

 举栗子:

import tkinter
import tkinter.messagebox

top = tkinter.Tk()

def hello():
    tkinter.messagebox.showinfo("hello Python","welcome here")

b = tkinter.Button(top,text = "click me", command = hello)
#将小部件放置到主窗口中 b.pack()
#进入消息循环
top.mainloop()

输出结果截图:

   

    点击按钮后弹出对话框

   

2、Entry(文本框)

用来让用户输入一行文本字符串。

如果需要输入多行文本,可以使用 Text 组件。

如果需要显示一行或多行文本且不允许用户修改,你可以使用 Label 组件。

t = Entry(master,option, ...)

文本框组件常用方法:

 

举栗子:

from tkinter import *

top = Tk()

L1 = Label(top, text = "姓名:")
L1.pack(side = LEFT)
E1 = Entry(top, bd=5)
E1.pack(side = RIGHT)
 
top.mainloop()

结果截图:

from tkinter import *

top = Tk()

number = ['0','1','2','3','4']
letter = ['a','b','c','d','e']

#创建两个列表组件
listbox1 = Listbox(top)
listbox2 = Listbox(top)

for item in number:
    listbox1.insert(0,item)
    
for item in letter:
    listbox2.insert(0,item)

listbox1.pack()
listbox2.pack()
top.mainloop()

结果截图:

猜你喜欢

转载自www.cnblogs.com/sheng-yang/p/10596947.html