python

Tkinter模块("Tk 接口")是Python的标准Tk GUI工具包的接口,位Python的内置模块,直接import tkinter即可使用。

1.创建窗口

from Tkinter import *   #引用Tk模块
root = Tk()             #初始化Tk()
root.mainloop()         #进入消息循环

几个常用属性

  • title: 设置窗口标题
  • geometry: 设置窗口大小
  • resizable():设置窗口是否可以变化长 宽

2.模块详解

#!/usr/bin/python
# -*- coding: utf-8 -*-
from Tkinter import *

root = Tk()
root.title("test Tkinter")

#### Text() ##################################################################################
root.geometry('300x100')
root.resizable(width=False,height=True)

#============================================
# 向空间内输入文本 Text
# 
# t = Text(根对象)
# 插入:t.insert(mark, 内容)
# 删除:t.delete(mark1, mark2)
# 其中,mark可以是行号,或者特殊标识
#============================================
t = Text(root)
t.insert(1.0, 'hello
')
t.insert(END, 'hello000000
')
t.insert(END, 'nono')
t.pack()


#### Entry() ##################################################################################
root.geometry()

#===================================================================
# 创建单行文本框 Entry
# 
# 创建:lb =Entry(根对象, [属性列表])
# 绑定变量 var=StringVar()    lb=Entry(根对象, textvariable = var)
# 获取文本框中的值   var.get()
# 设置文本框中的值   var.set(item1)
#===================================================================
var = StringVar()
e = Entry(root, textvariable = var)
var.set("hello")
e.pack()

#### Listbox() ################################################################################
root.geometry()

# ======================================================================
# 
# 列表控件,可以含有一个或多个文本想,可单选也可多选 Listbox
# 创建:lb = ListBox(根对象, [属性列表])
# 绑定变量 var=StringVar()    lb=ListBox(根对象, listvariable = var)
# 得到列表中的所有值   var.get()
# 设置列表中的所有值   var.set((item1, item2, .....))
# 添加:lb.insert(item)
# 删除:lb.delete(item,...) item为序号
# 绑定事件 lb.bind('<ButtonRelease-1>', 函数)
# 获得所选中的选项 lb.get(lb.curselection())
# selectmode可以为BROWSE MULTIPL SINGLE
# ======================================================================
def print_item(event):
    print lb.get(lb.curselection())
var = StringVar()
lb = Listbox(root, listvariable=var)
list_item = [1,2,3,4]
for item in list_item:
    lb.insert(END, item)
lb.delete(2,4)

var.set(('a', 'ab', 'c', 'd'))
print var.get()
lb.bind('<ButtonRelease-1>', print_item)
lb.pack()

#### Scrollbar ################################################################################
root.geometry()

# ================================================================
# 滚动条控件,当内容超过可视化区域时使用,如列表框。
# 
# Frame(根对象, [属性列表]), 最长用的用法是和别的控件一起使用.
# ================================================================
def print_item(event):
    print lb.get(lb.curselection())
var = StringVar()
lb = Listbox(root, selectmode=BROWSE, listvariable=var)
lb.bind('<ButtonRelease-1>', print_item)
list_item = [1,2,3,4,5,6,7,8,9,0]
for item in list_item:
    lb.insert(END, item)

scrl = Scrollbar(root)
scrl.pack(side=RIGHT, fill=Y)
lb.configure(yscrollcommand=scrl.set)
lb.pack(side=LEFT, fill=BOTH)
scrl['command'] = lb.yview


# 进入消息循环
root.mainloop()

3.pack 函数详解

'''''fill如何控制子组件的布局'''  
# -*- coding: utf-8 -*-  
# 不设置root的大小,使用默认  
from tkinter import *  
  
root = Tk()  
# 改变root的大小为80x80  
root.geometry('80x80+0+0')  
print(root.pack_slaves())  
# 创建三个Label分别使用不同的fill属性  
Label(root, text='pack1', bg='red').pack(fill=Y)  
Label(root, text='pack2', bg='blue').pack(fill=BOTH)  
Label(root, text='pack3', bg='green').pack(fill=X)  
print(root.pack_slaves())  
root.mainloop()  
# 第一个只保证在Y方向填充,第二个保证在XY两个方向上填充,第三个不使用填充属性,  
# 注意Pack只会吝啬地给出可以容纳这三个组件的最小区域,它不允许使用剩余的空间了,故下方留有“空白”。  
'''''expand如何控制组件的布局'''  
# -*- coding: utf-8 -*-  
# 这个属性指定如何使用额外的空间,即上例中留下来的“空白”  
from tkinter import *  
  
root = Tk()  
# 改变root的大小为80x80  
root.geometry('80x80+0+0')  
print(root.pack_slaves())  
# 创建三个Label分别使用不同的fill属性  
Label(root, text='pack1', bg='red').pack(fill=Y, expand=1)  
Label(root, text='pack2', bg='blue').pack(fill=BOTH, expand=1)  
Label(root, text='pack3', bg='green').pack(fill=X, expand=0)  
print(root.pack_slaves())  
root.mainloop()  
# 第一个只保证在Y方向填充,第二个保证在XY两个方向上填充,第三个不使用填充属性,  
# 这个例子中第一个Label和第二个Label使用了expand = 1属性,而第三个使用expand = 0属性,改变root的大小,  
# 可以看到Label1和Label2是随着root的大小变化而变化(严格地它的可用空间在变化),第三个只中使用fill进行X方向上的填充,不使用额外的空间。  
'''''改变组件的排放位置'''  
# 使用side属性改变放置位置  
# -*- coding: utf-8 -*-  
from tkinter import *  
  
root = Tk()  
# 改变root的大小为80x80  
root.geometry('80x80+0+0')  
print(root.pack_slaves())  
# 创建三个Label分别使用不同的fill属性,改为水平放置  
# 将第一个Label居左放置  
Label(root, text='pack1', bg='red').pack(fill=Y, expand=1, side=LEFT)  
# 将第二个Label居右放置  
Label(root, text='pack2', bg='blue').pack(fill=BOTH, expand=1, side=RIGHT)  
# 将第三个Label居左放置,靠Label放置,注意它不会放到Label1的左边  
Label(root, text='pack3', bg='green').pack(fill=X, expand=0, side=LEFT)  
print(root.pack_slaves())  
root.mainloop()  
# 第一个只保证在Y方向填充,第二个保证在XY两个方向上填充,第三个不使用填充属性,这个例子中第一个Label和第二个Label使用了expand = 1属性,  
# 而第三个使用expand = 0属性,改变root的大小,可以看到Label1和Label2是随着root的大小变化而变化(严格地它的可用空间在变化),  
# 第三个只中使用fill进行X方向上的填充,不使用额外的空间。  
'''''设置组件之间的间隙大小'''  
# ipadx设置内部间隙  
# padx设置外部间隙  
# -*- coding: utf-8 -*-  
# 不设置root的大小,使用默认  
from tkinter import *  
  
root = Tk()  
# 改变root的大小为80x80  
# root.geometry('80x80+0+0')  
print(root.pack_slaves())  
# 创建三个Label分别使用不同的fill属性,改为水平放置  
# 将第一个LabelFrame居左放置  
L1 = LabelFrame(root, text='pack1', bg='red')  
# 设置ipadx属性为20  
L1.pack(side=LEFT, ipadx=20)  
Label(L1,  
      text='inside',  
      bg='blue'  
      ).pack(expand=1, side=LEFT)  
L2 = Label(root,  
           text='pack2',  
           bg='blue'  
           ).pack(fill=BOTH, expand=1, side=LEFT, padx=10)  
L3 = Label(root,  
           text='pack3',  
           bg='green'  
           ).pack(fill=X, expand=0, side=LEFT, pady=10)  
print(root.pack_slaves())  
root.mainloop()  
# 为了演示ipadx/padx,创建了一个LabelFrame设置它的ipadx为20,即内部间隔值为20,  
# 它的子组件若使用则会留出20个单位;Label2和Label3分别设置x和y方向上的外部间隔值,所有与之排列的组件会与之保留10个单位值的距离  

4.PaneWindow 详解

from Tkinter import *  

#### PanedWindow #####################################################################
# PanedWindow是一个窗口布局管理的插件,可以包含一个或者多个子控件。
# 用户可以用鼠标移动上面的分割线来改变每个子控件的大小。

PanedWindow可以用来创建2格或者3格的布局。
m1 = PanedWindow()  
m1.pack(fill=BOTH, expand=1)  
  
left = Label(m1, text="left pane", bg="red")  
m1.add(left)  
  
m2 = PanedWindow(m1, orient=VERTICAL)  
m1.add(m2)  
  
top = Label(m2, text="top pane", bg="blue")  
m2.add(top)  
  
bottom = Label(m2, text="bottom pane", bg="yellow")  
m2.add(bottom)  
  
mainloop()

参考自:

  Tkinter 说明:http://www.runoob.com/python/python-gui-tkinter.html

  Tkinter 内容详解:https://www.cnblogs.com/kaituorensheng/p/3287652.html

  pack 函数详解:http://blog.csdn.net/aa1049372051/article/details/51886909

原文地址:https://www.cnblogs.com/blitheG/p/7844983.html