Tkinter 学习笔记

Tkinter 学习笔记

Tkinter是什么

  • 摘自wiki

    Tkinter is a Python binding to the Tk GUI toolkit. It is the standard Python interface to the Tk GUI toolkit, and is Python’s de facto standard GUI. Tkinter is included with standard Linux, Microsoft Windows and Mac OS X installs of Python.

  • 简单来说Tkinter库就是一个调用 TK GUI 工具库进行gui程序编写的库,具有多平台的支持

使用

  • 用Tkinter编写的gui程序基本的编写思路非常简单:

    1. 创建一个主窗口
    2. 创建各种frame(框架)和widget(组件),其间可以通过command参数或bind方法为widget指定回调函数,如按下按钮后要执行的操作等等
    3. 通过不同的布局管理方式组织widget和frame在主窗口中的布局
    4. 执行mainloop,进行各种事件的监听
  • 从上面可以看出,用Tkinter编程的思路很简单,我们学习的关键就是弄清楚各种类、类方法的含义和使用方法

  • 后文将记录笔者在学习过程中的学到的Tkinter中各种类的使用方法

widget

  • Tkinter中有丰富的widget,查看源码后可以发现所有的widget子类,即Button, Text这些,都是通过继承父类widget实现的

通用的方法

config

  • config用于在运行过程中改变widget的属性,举例:
    import tkinter as tk
    
    def func():
        b.config(text="clicked")
    
    m = tk.Tk()
    b = tk.Button(m, text="not clicked", command=func, width=10, height=15)
    b.pack()
    tk.mainloop()
    
    点击按钮后,按钮文字由“not clicked”变为“clicked”

pack

  • pack方法会自动的对当前widget进行布局管理,默认为由上至下,由左至右
  • 可选参数
    • expand :接收一个boolean,为True时,该组件会占用其所在框架的全部未使用的空间,即尽可能的大,默认为False
    • fill:
      fill − Determines whether widget fills any extra space allocated to it by the packer, or keeps its own minimal dimensions: NONE (default), X (fill only horizontally), Y (fill only vertically), or BOTH (fill both horizontally and vertically).

side − Determines which side of the parent widget packs against: TOP (default), BOTTOM, LEFT, or RIGHT.

Button

Listbox

  • 回调函数x/yscrollcommand:接收两个浮点数,具体含义没有查到,但一般会将这两个回调函数指向Scrollbarset方法,用于将ListBox窗口的移动反应在Scrollbar

Scrollbar

  • 回调函数command:指向要通过该Scrollbar进行调整的widget(例如ListboxText等)的yview函数

after
update
variable

你可能感兴趣的:(gui,tkinter,python,gui,tkinter,python)