0%

Tkinter

Tkinter

Python提供了多个图形开发界面的库

  • Tkinter

    Tkinter模块(“Tk接口”)是Python的标准Tk GUI工具包的接口。Tk和Tkinter可以在大多数的Unix平台下使用,同样可以应用在Windows和Macintosh系统里,Tk8.0的后续版本可以实现本地窗口风格,并良好地运行在绝大多数平台中。

  • wxPython

    wxPython是一款开源软件,是Python语言的一套优秀的GUI图形库,允许Python程序员很方便的创建完整的、功能键全的GUI用户界面。

  • Jython

    Jython程序可以和Java无缝集成。除了一些标准模块,Jython使用Java的模块。Jython几乎拥有标准的Python中不依赖于C语言的全部模块。比如,Jython的用户界面将使用Swing,AWT或者SWT。Jython可以被动态或静态地编译成Java字节码。

Tkinter编程

  • 说明

    Tkinter是内置到python的安装包中、只要安装好Python之后就能import tkinter库、而且IDLE也是用Tkinter编写而成

  • 步骤

    1. 导入Tkinter模块
    2. 创建控件
    3. 指定这个控件的master,即这个控件属于哪一个
    4. 告诉GM(geometry manager)有一个控件产生了

简单示例

1
2
3
4
5
6
7
8
9
10
11
12
import tkinter

# 创建主窗口
win = tkinter.Tk()
# 设置标题
win.title('Hello')
# 设置大小和位置,大小:400x400,位置:+200+200
win.geometry('400x400+200+200')

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

Tkinter控件

  • Label控件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    import tkinter

    win = tkinter.Tk()
    win.title('Label')
    win.geometry("400x400+200+200")

    '''
    Label:标签控件可以显示文本
    参数:
    win 父窗体
    text 显示的文本内容
    bg 背景色
    fg 字体颜色
    font (字体, 大小)
    width 宽度
    height 高度
    wraplength 指定text文本中多宽进行换行
    justify 设置换行后的对齐方法,left、right、center
    anchor 位置:n上北 s下南 w左西 e右东 c(center)居中 ne se sw nw
    '''

    label = tkinter.Label(
    win,
    text='天下无双',
    bg='blue',
    fg='white',
    font=('楷体', 20),
    width=30,
    height=5,
    wraplength=10,
    justify='left',
    anchor='c',
    )

    # 显示出来
    label.pack()

    win.mainloop()

  • Button控件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import os
    import tkinter


    def func():
    print('天下无双')


    win = tkinter.Tk()
    win.title('Button')
    win.geometry("400x400+200+200")

    # 创建按钮
    button1 = tkinter.Button(win, width=10, text='按钮1', command=func)
    button1.pack()

    button2 = tkinter.Button(win, width=10, text='按钮2', command=lambda: print('国士无双'))
    button2.pack()

    # 打开记事本
    button3 = tkinter.Button(win, width=10, text='notepad', command=lambda: os.system('notepad'))
    button3.pack()

    button4 = tkinter.Button(win, width=10, text='退出', command=win.quit)
    button4.pack()

    win.mainloop()

  • Entry控件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    import tkinter

    # 创建主窗口
    win = tkinter.Tk()
    # 设置标题
    win.title('Entry')
    # 设置大小和位置
    win.geometry('400x400-200-200')

    '''
    输入控件
    用于显示简单的文本内容
    '''

    # 绑定变量
    e = tkinter.Variable()
    # show 密文显示 show='*'
    entry = tkinter.Entry(win, textvariable=e)
    entry.pack()

    # e就代表输入框这个对象
    # 设置值
    e.set('请输入密码')
    # 取值
    print(e.get())
    print(entry.get())

    win.mainloop()

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    import tkinter

    # 创建主窗口
    win = tkinter.Tk()
    # 设置标题
    win.title('Entry+Button')
    # 设置大小和位置
    win.geometry('400x400-200-200')


    def showInfo():
    print(entry.get())


    entry = tkinter.Entry(win)
    entry.pack()

    button = tkinter.Button(win, width=10, text="显示输入", command=showInfo)
    button.pack()

    win.mainloop()

    点击按钮输出输入框中的内容

  • Text控件

    • 不带滚动条

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      import tkinter

      # 创建主窗口
      win = tkinter.Tk()
      # 设置标题
      win.title('Text')
      # 设置大小和位置
      win.geometry('400x400-200-200')

      '''
      文本控件,用于显示多行文本
      '''

      # height显示的行数
      text = tkinter.Text(win, width=30, height=4)
      text.pack()
      str = '''If there is anyone out there who still doubts that America is a place where all things are possible, who still wonders if the dream of our founders is alive in our time, who still questions the power of our democracy, tonight is your answer.'''
      text.insert(tkinter.INSERT, str)

      win.mainloop()

    • 带滚动条

      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      import tkinter

      # 创建主窗口
      win = tkinter.Tk()
      # 设置标题
      win.title('Text')
      # 设置大小和位置
      win.geometry('-300-400')

      '''
      文本控件,用于显示多行文本
      '''

      # 创建滚动条
      scroll = tkinter.Scrollbar()
      # 创建Text
      text = tkinter.Text(win, width=50, height=8)
      # side放到窗体的那一侧 fill填充
      scroll.pack(side=tkinter.RIGHT, fill=tkinter.Y)
      text.pack(side=tkinter.LEFT, fill=tkinter.Y)
      # 关联
      scroll.config(command=text.yview)
      text.config(yscrollcommand=scroll.set)

      str = '''If there is anyone out there who still doubts that America is a place where all things are possible, who still wonders if the dream of our founders is alive in our time, who still questions the power of our democracy, tonight is your answerIt’s the answer told by lines that stretched around schools and churches in numbers this nation has never seen, by people who waited three hours and four hours, many for the first time in their lives, because they believed that this time must be different, that their voices could be that difference.'''
      text.insert(tkinter.INSERT, str)

      win.mainloop()

  • CheckButton控件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    import tkinter

    win = tkinter.Tk()
    win.title('CheckButton')
    win.geometry('400x400-200-200')


    def update():
    message = ''
    if hobby1.get() == True:
    message += 'money\n'
    if hobby2.get() == True:
    message += 'power\n'
    if hobby3.get() == True:
    message += 'people\n'

    # 清除text中的所有内容
    text.delete(0.0, tkinter.END)
    text.insert(tkinter.INSERT, message)


    # 要绑定的变量
    hobby1 = tkinter.BooleanVar()
    hobby2 = tkinter.BooleanVar()
    hobby3 = tkinter.BooleanVar()
    # 多选框
    check1 = tkinter.Checkbutton(win, text='money', variable=hobby1, command=update)
    check2 = tkinter.Checkbutton(win, text='power', variable=hobby2, command=update)
    check3 = tkinter.Checkbutton(win, text='people', variable=hobby3, command=update)
    check1.pack()
    check2.pack()
    check3.pack()

    text = tkinter.Text(win, width=50, height=5)
    text.pack()

    win.mainloop()

  • RadioButton控件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    import tkinter

    win = tkinter.Tk()
    win.title('RadioButton')
    win.geometry('400x400-200-200')


    def update():
    print(r.get())


    # 一组单选框要绑定同一个变量
    r = tkinter.IntVar()

    radio1 = tkinter.Radiobutton(win, text='one', value=1, variable=r, command=update)
    radio2 = tkinter.Radiobutton(win, text='two', value=2, variable=r, command=update)
    radio3 = tkinter.Radiobutton(win, text='three', value=3, variable=r, command=update)

    radio1.pack()
    radio2.pack()
    radio3.pack()

    win.mainloop()

  • Listbox控件

    1. Listbox控件上(BROWSE)
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      37
      38
      39
      40
      41
      42
      43
      44
      45
      46
      47
      48
      49
      50
      51
      52
      import tkinter

      win = tkinter.Tk()
      win.title('Listbox-BROWSE')
      win.geometry('400x400-200-200')

      '''
      列表框控件,可以包含一个或者多个文本框
      作用:在listbox控件的小窗口显示一个字符串
      '''

      # 创建一个listbox, 添加几个元素
      lb = tkinter.Listbox(win, selectmode=tkinter.BROWSE)
      lb.pack()
      for item in ['good', 'nice', 'handsome', 'vg', 'vn']:
      # 按顺序添加
      lb.insert(tkinter.END, item)

      # 在开始添加
      lb.insert(tkinter.ACTIVE, 'cool')

      # 将列表当成一个元素添加的
      lb.insert(tkinter.END, ['very good', 'very nice'])

      # 删除 参数1为开始的索引,参数2为结束的索引,如果不指定参数2,只删除第一个索引处的内容
      # lb.delete(1,3)
      # lb.delete(1)

      # 选中 参数1为开始的索引,参数2为结束的索引,如果不指定参数2,只选中第一个索引处的内容
      lb.select_set(2, 5)
      # lb.select_set(2)

      # 取消
      # lb.select_clear(2,4)
      # lb.select_clear(3)

      # 获取到列表中的元素的个数
      # print(lb.size())

      # 从列表中取值 参数1为开始的索引,参数2为结束的索引,如果不指定参数2,只获取第一个索引处的内容
      # print(lb.get(2, 4))
      # print(lb.get(2))

      # 返回当前的索引项,不是item元素
      print(lb.curselection())

      # 判断一个选项是否被选中
      print(lb.selection_includes(1))
      print(lb.selection_includes(3))

      win.mainloop()

    2. Listbox控件中(SINGLE)
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      import tkinter

      win = tkinter.Tk()
      win.title('Listbox-SINGLE')
      win.geometry('400x400-200-200')

      # 绑定变量
      lbv = tkinter.StringVar()
      # 与BROWSE相似,但是不支持鼠标按下后移动选中位置
      lb = tkinter.Listbox(win, selectmode=tkinter.SINGLE, listvariable=lbv)
      lb.pack()
      for item in ['good', 'nice', 'handsome', 'vg', 'vn']:
      # 按顺序添加
      lb.insert(tkinter.END, item)

      # 打印当前列表中的选项
      print(lbv.get())


      # 设置选项
      # lbv.set(('1', '2', '3'))


      # 绑定事件
      def myPrint(event):
      print(lb.get(lb.curselection()))


      lb.bind('<Double-Button-1>', myPrint)

      win.mainloop()

    3. Listbox控件中下(EXTENDED)
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      import tkinter

      win = tkinter.Tk()
      win.title('Listbox-EXTENDED')
      win.geometry('-200-200')

      # EXTENDED 可以使listbox支持shift和control
      # 按住shift,可以实现连选
      # 按住control,可以实现多选
      lb = tkinter.Listbox(win, selectmode=tkinter.EXTENDED)
      for item in ['good', 'nice', 'handsome', 'vg', 'vn', 'good1', 'nice1', 'handsome1', 'vg1', 'vn1', 'good3', 'nice3',
      'handsome3', 'vg3', 'vn3']:
      lb.insert(tkinter.END, item)

      # 滚动条
      sc = tkinter.Scrollbar(win)
      sc.pack(side=tkinter.RIGHT, fill=tkinter.Y)
      lb.configure(yscrollcommand=sc.set)
      lb.pack(side=tkinter.LEFT, fill=tkinter.BOTH)
      # 额外给属性赋值
      sc['command'] = lb.yview

      win.mainloop()

    4. Listbox控件下(MULTIPLE)
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      import tkinter

      win = tkinter.Tk()
      win.title('Listbox-MULTIPLE')
      win.geometry('400x400-200-200')

      # MULTIPLE 支持多选
      lb = tkinter.Listbox(win, selectmode=tkinter.MULTIPLE)
      lb.pack()
      for item in ['good', 'nice', 'handsome', 'vg', 'vn']:
      lb.insert(tkinter.END, item)

      win.mainloop()

  • Scale控件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    import tkinter

    win = tkinter.Tk()
    win.title('Scale')
    win.geometry('400x400-200-200')

    '''
    供用户通过拖拽指示器改变变量的值,可以水平,也可以竖直
    tkinter.HORIZONTAL 水平
    tkinter.VERTICAL 竖直
    length 水平时表示宽度,竖直时表示高度
    tickinterval 选择值将会为该值的倍数
    '''
    scale = tkinter.Scale(win, from_=0, to=100, orient=tkinter.HORIZONTAL, tickinterval=20, length=200)
    scale.pack()

    # 设置值
    scale.set(20)


    # 取值
    def showNum():
    print(scale.get())


    tkinter.Button(win, text='取值', command=showNum).pack()

    win.mainloop()

  • Spinbox控件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    import tkinter

    win = tkinter.Tk()
    win.title('Spinbox')
    win.geometry('400x400-200-200')

    '''
    数值范围控件
    '''


    def update():
    print(v.get())


    # 绑定个变量
    v = tkinter.StringVar()

    # increment 步长 默认为1
    # values 最好不要与from_=0, to=100和increment=2同时使用
    # values=(0, 2, 4, 6, 8)
    # command 只要值改变就会执行对应的方法
    sp = tkinter.Spinbox(win, from_=0, to=100, increment=1, textvariable=v, command=update)
    sp.pack()

    # 赋值
    v.set(20)
    # 取值
    print(v.get())

    win.mainloop()

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      import tkinter

      win = tkinter.Tk()
      win.title('Menu顶层菜单')
      win.geometry('400x400-200-200')

      # 菜单条
      menubar = tkinter.Menu(win)
      win.config(menu=menubar)


      def func():
      print('顶层菜单')


      # 创建一个菜单选项
      menu1 = tkinter.Menu(menubar, tearoff=False)
      # 给菜单选项添加内容
      for item in ['Python', 'C', 'C++', 'OC', 'Swift', 'C#', 'shell', 'Java', 'JS', 'PHP', '汇编', 'NodeJS', '退出']:
      if item == '退出':
      # 添加分隔线
      menu1.add_separator()
      menu1.add_command(label=item, command=win.quit)
      else:
      menu1.add_command(label=item, command=func)

      # 向菜单条上添加菜单选项
      menubar.add_cascade(label='语言', menu=menu1)

      menu2 = tkinter.Menu(menubar, tearoff=False)
      menu2.add_command(label='深色主题')
      menu2.add_command(label='浅色主题')
      menubar.add_cascade(label='主题', menu=menu2)

      win.mainloop()

    • 1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      import tkinter

      win = tkinter.Tk()
      win.title('Menu鼠标右键菜单')
      win.geometry('400x400-200-200')

      # 菜单条
      menubar = tkinter.Menu(win)

      # 菜单
      menu = tkinter.Menu(menubar, tearoff=False)
      for item in ["Python", "C", "C++", "OC", "Swift", "C#", "shell", "Java", "JS", "PHP", "汇编", "NodeJS", "退出"]:
      menu.add_command(label=item)

      menubar.add_cascade(label="语言", menu=menu)


      def showMenu(event):
      menubar.post(event.x_root, event.y_root)


      win.bind("<Button-3>", showMenu)

      win.mainloop()

  • Combobox控件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import tkinter
    from tkinter import ttk

    win = tkinter.Tk()
    win.title('Combobox')
    win.geometry('400x400-200-200')

    # 绑定变量
    cv = tkinter.StringVar()
    com = ttk.Combobox(win, textvariable=cv)
    com.pack()
    # 设置下拉数据
    com['value'] = ('黑龙江', '吉林', '辽宁', '广州', '深圳', '北京', '上海')

    # 设置默认值
    com.current(5)


    # 绑定事件
    def func(event):
    print(com.get())
    print(cv.get())


    com.bind('<<ComboboxSelected>>', func)

    win.mainloop()

  • Frame控件

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    import tkinter

    win = tkinter.Tk()
    win.title('Frame')
    win.geometry('400x400-200-200')

    '''
    框架控件
    作用:在屏幕上显示一个矩形区域,多作为容器控件
    '''

    frm = tkinter.Frame(win)
    frm.pack()

    # left
    frm_l = tkinter.Frame(frm)
    tkinter.Label(frm_l, text='左上', bg='pink').pack(side=tkinter.TOP)
    tkinter.Label(frm_l, text='左下', bg='blue').pack(side=tkinter.TOP)
    frm_l.pack(side=tkinter.LEFT)

    # Right
    frm_r = tkinter.Frame(frm)
    tkinter.Label(frm_r, text='右上', bg='red').pack(side=tkinter.TOP)
    tkinter.Label(frm_r, text='右下', bg='yellow').pack(side=tkinter.TOP)
    frm_r.pack(side=tkinter.RIGHT)

    win.mainloop()

    Frame

数据显示

  • 表格数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    import tkinter
    from tkinter import ttk

    win = tkinter.Tk()
    win.title('表格数据')
    win.geometry('600x400-200-200')

    # 表格
    tree = ttk.Treeview(win)
    tree.pack()

    # 定义列
    tree['columns'] = ('姓名', '年龄', '身高', '体重')
    # 设置列,列还不显示
    tree.column('姓名', width=100)
    tree.column('年龄', width=100)
    tree.column('身高', width=100)
    tree.column('体重', width=100)

    # 设置表头
    tree.heading('姓名', text='姓名(name)')
    tree.heading('年龄', text='年龄(age)')
    tree.heading('身高', text='身高(height)')
    tree.heading('体重', text='体重(weight)')

    # 添加数据
    tree.insert('', 0, text='line1', values=('陆彦旭', '28', '165', '80'))
    tree.insert('', 1, text='line2', values=('范育宾', '29', '167', '70'))

    win.mainloop()

    表格数据

  • 树状数据

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    import tkinter
    from tkinter import ttk

    win = tkinter.Tk()
    win.title('树状数据')
    win.geometry('400x400-200-200')

    tree = ttk.Treeview(win, height=15)
    tree.pack()

    # 添加一级树枝
    treeF1 = tree.insert('', 0, '中国', text='中国CHI', values=('F1'))
    treeF2 = tree.insert('', 1, '美国', text='美国USA', values=('F2'))
    treeF3 = tree.insert('', 2, '英国', text='英国ENG', values=('F3'))

    # 二级树枝
    treeF1_1 = tree.insert(treeF1, 0, '黑龙江', text='中国黑龙江', values=('F1_1'))
    treeF1_2 = tree.insert(treeF1, 1, '吉林', text='中国吉林', values=('F1_2'))
    treeF1_3 = tree.insert(treeF1, 2, '辽宁', text='中国辽宁', values=('F1_3'))

    treeF2_1 = tree.insert(treeF2, 0, '德克萨斯州', text='美国德克萨斯州', values=('F2_1'))
    treeF2_2 = tree.insert(treeF2, 1, '底特律', text='美国底特律', values=('F1_2'))
    treeF2_3 = tree.insert(treeF2, 2, '旧金山', text='美国旧金山', values=('F1_3'))

    # 三级树枝
    treeF1_1_1 = tree.insert(treeF1_1, 0, '哈尔滨', text='黑龙江哈尔滨')
    treeF1_1_2 = tree.insert(treeF1_1, 1, '五常', text='黑龙江五常')

    win.mainloop()

    树状数据

布局

  • 绝对布局(place)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    import tkinter

    win = tkinter.Tk()
    win.title('绝对布局')
    win.geometry('400x400-200-200')

    label1 = tkinter.Label(win, text='good', bg='orange')
    label2 = tkinter.Label(win, text='nice', bg='red')
    label3 = tkinter.Label(win, text='cool', bg='pink')

    # 绝对布局,窗口的变化对位置没有影响
    label1.place(x=10, y=10)
    label2.place(x=50, y=50)
    label3.place(x=100, y=100)

    win.mainloop()

    绝对布局

  • 相对布局(pack)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    import tkinter

    win = tkinter.Tk()
    win.title('相对布局')
    win.geometry('400x400-200-200')

    label1 = tkinter.Label(win, text='good', bg='orange')
    label2 = tkinter.Label(win, text='nice', bg='red')
    label3 = tkinter.Label(win, text='handsome', bg='pink')
    label4 = tkinter.Label(win, text='beautiful', bg='green')

    # 相对布局,窗体改变对控件有影响
    # tkinter.BOTH
    label1.pack(fill=tkinter.Y, side=tkinter.LEFT)
    label2.pack(fill=tkinter.Y, side=tkinter.RIGHT)
    label3.pack(fill=tkinter.X, side=tkinter.TOP)
    label4.pack(fill=tkinter.X, side=tkinter.BOTTOM)

    win.mainloop()

    相对布局

  • 表格布局(grid)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    import tkinter

    win = tkinter.Tk()
    win.title('表格布局')
    win.geometry('400x400-200-200')

    label1 = tkinter.Label(win, text='good', bg='orange', width=10)
    label2 = tkinter.Label(win, text='wonderful', bg='red', width=10)
    label3 = tkinter.Label(win, text='cool', bg='pink', width=10)
    label4 = tkinter.Label(win, text='handsome', bg='yellow', width=10)

    # 表格布局
    label1.grid(row=0, column=0)
    label2.grid(row=0, column=1)
    label3.grid(row=1, column=0)
    label4.grid(row=1, column=1)

    win.mainloop()

    表格布局

事件

事件

事件属性

  • 鼠标事件

    • 鼠标点击事件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      import tkinter

      win = tkinter.Tk()
      win.title('鼠标点击事件')
      win.geometry('400x400-200-200')

      '''
      <Button-1> 鼠标左键
      <Button-3> 鼠标右键
      <Button-2> 鼠标中键
      <Double-Button-1> 鼠标左键双击
      <Double-Button-3> 鼠标右键双击
      <Double-Button-2> 鼠标中键双击
      <Triple-Button-1> 鼠标左键三击
      <Triple-Button-3> 鼠标右键三击
      <Triple-Button-2> 鼠标中键三击
      '''


      def func(event):
      print(event.x, event.y)


      button = tkinter.Button(win, text='leftMouse button')
      # bind 给控件绑定事件
      button.bind('<Triple-Button-1>', func)
      button.pack()

      win.mainloop()

    • 鼠标移动事件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      import tkinter

      win = tkinter.Tk()
      win.title('鼠标移动事件')
      win.geometry('400x400-200-200')

      '''
      <B1-Motion> 左键移动
      <B3-Motion> 右键移动
      <B2-Motion> 中键移动
      '''

      label = tkinter.Label(win, text='鼠标移动事件', bg='red')
      label.pack()


      def func(event):
      print(event.x, event.y)


      label.bind('<B1-Motion>', func)

      win.mainloop()

    • 鼠标释放事件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      import tkinter

      win = tkinter.Tk()
      win.title('鼠标释放事件')
      win.geometry('400x400-200-200')

      '''
      <ButtonRelease-1> 释放鼠标左键
      <ButtonRelease-3> 释放鼠标右键
      <ButtonRelease-2> 释放鼠标中键
      '''

      label = tkinter.Label(win, text='鼠标释放事件', bg='red')
      label.pack()


      def func(event):
      print(event.x, event.y)


      label.bind('<ButtonRelease-1>', func)

      win.mainloop()

    • 鼠标进入和离开事件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      import tkinter

      win = tkinter.Tk()
      win.title('鼠标进入和离开事件')
      win.geometry('400x400-200-200')

      '''
      <Enter> 鼠标光标进入控件时触发
      <Leave> 鼠标光标离开控件时触发
      '''

      label = tkinter.Label(win, text='鼠标进入和离开事件', bg='red')
      label.pack()


      def func(event):
      print(event.x, event.y)


      label.bind('<Leave>', func)

      win.mainloop()

  • 按键事件

    • 响应所有按键事件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      34
      35
      36
      import tkinter

      win = tkinter.Tk()
      win.title('响应所有按键事件')
      win.geometry('400x400-200-200')

      '''
      <Key> 响应所有的按键
      '''

      label = tkinter.Label(win, text='响应所有按键事件', bg='red')

      '''
      # 设置焦点
      label.focus_set()
      label.pack()


      def func(event):
      print('event.char =', event.char)
      print('event.keycode =', event.keycode)


      label.bind('<Key>', func)
      '''


      def func(event):
      print('event.char =', event.char)
      print('event.keycode =', event.keycode)


      win.bind('<Key>', func)

      win.mainloop()

    • 响应特殊按键事件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      23
      24
      25
      26
      27
      28
      29
      30
      31
      32
      33
      import tkinter

      win = tkinter.Tk()
      win.title('响应特殊按键事件')
      win.geometry('400x400-200-200')

      '''
      <Alt_L> 左Alt
      <Alt_R> 右Alt
      <Shift_L> 左shift
      <Shift_R> 右shift
      <Control_L> 左Control
      <Control_R> 右Control
      <F5> F5
      <Return> 回车
      <BackSpace> 退格
      '''

      label = tkinter.Label(win, text='响应特殊按键事件', bg='red')
      # 设置焦点
      label.focus_set()
      label.pack()


      def func(event):
      print('event.char =', event.char)
      print('event.keycode =', event.keycode)


      label.bind('<BackSpace>', func)

      win.mainloop()

    • 指定按键事件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      import tkinter

      win = tkinter.Tk()
      win.title('指定按键事件')
      win.geometry('400x400-200-200')


      def func(event):
      print('event.char =', event.char)
      print('event.keycode =', event.keycode)


      win.bind('a', func)

      win.mainloop()

    • 组合按键事件
      1
      2
      3
      4
      5
      6
      7
      8
      9
      10
      11
      12
      13
      14
      15
      16
      17
      18
      19
      20
      21
      22
      import tkinter

      win = tkinter.Tk()
      win.title('组合按键事件')
      win.geometry('400x400-200-200')

      '''
      <Control-Alt-c>
      <Shift-Up>
      <Control-p>
      '''


      def func(event):
      print('event.char =', event.char)
      print('event.keycode =', event.keycode)


      win.bind('<Control-Alt-c>', func)

      win.mainloop()