pythone tkinter 学习笔记1

    技术2022-07-14  85

    目录

    1. 帮助

    2. Tk 命令行

    2.1. EXAMPLES

    2.2. Tk命令行

    3. Tk to python tkinter


    tkinter: tk interface Tcl/Tk:

    1. 帮助

    https://www.tcl.tk/man/tcl8.6/TkCmd/contents.htm

    https://docs.python.org/3.0/library/tkinter.html

     

    2. Tk 命令行

    2.1. EXAMPLES

    http://www.tcl.tk/man/tcl8.6/TkCmd/grid.htm A toplevel window containing a text widget and two scrollbars:

    # Make the widgets toplevel .t text .t.txt -wrap none -xscroll {.t.h set} -yscroll {.t.v set} scrollbar .t.v -orient vertical -command {.t.txt yview} scrollbar .t.h -orient horizontal -command {.t.txt xview} # Lay them out grid .t.txt .t.v -sticky nsew grid .t.h -sticky nsew # Tell the text widget to take all the extra room grid rowconfigure .t .t.txt -weight 1 grid columnconfigure .t .t.txt -weight 1

    转换为 python tkinter

    #!/usr/bin/env python3 import tkinter import tkinter.ttk # Make the widgets top = tkinter.Tk() #toplevel .t #text .t.txt -wrap none -xscroll {.t.h set} -yscroll {.t.v set} top.txt = tkinter.Text(wrap='none') #scrollbar .t.v -orient vertical -command {.t.txt yview} #scrollbar .t.h -orient horizontal -command {.t.txt xview} top.v = tkinter.ttk.Scrollbar(orient='vertical', command=top.txt.yview) top.h = tkinter.ttk.Scrollbar(orient='horizontal', command=top.txt.xview) top.txt.configure(xscroll=top.h.set, yscroll=top.v.set) # Lay them out #grid .t.txt .t.v -sticky nsew #grid .t.h -sticky nsew top.txt.grid(row=0, column=0, sticky='nsew') top.v.grid(row=0, column=1, sticky='nsew') top.h.grid(row=1, column=0, sticky='nsew') # Tell the text widget to take all the extra room #grid rowconfigure .t .t.txt -weight 1 #grid columnconfigure .t .t.txt -weight 1 #### top 对象继承了 grid 的 rowconfigure 方法 #### Tk 一行可以指定多个对象, tkinter 只能写多行 top.rowconfigure(0, weight=1) top.columnconfigure(0, weight=1) top.txt.rowconfigure(0, weight=1) top.txt.columnconfigure(0, weight=1) top.mainloop()

    2.2. Tk命令行

    Tk命令行: classCommand newPathname options Tk命令行: oldPathname classCommand(configure) options

    .fred configure -fg red     =====>  fred["fg"] = red

    1.classCommand 指定widget的类型 (a button, a label, a menu…) 2.newPathname 是 widget的对象实例名 All names in Tk must be unique. To help enforce this, widgets in Tk are named with pathnames, just like files in a file system. 3.options 用于设置 widget的显示方式和行为 The options come in the form of a list of flags and values. Flags are preceded by a ‘-‘, like Unix shell command flags, and values are put in quotes if they are more than one word.

    转换为c 为: classCommand(newPathname, options); 转换为c++ 为: newPathname.classCommand(options); 转换为python 为: newPathname.classCommand(options);

    Tk 命令行: button   .fred   -fg red -text "hi there" button (Tk cmd)为 class类, .fred (Tk 路径)为创建的对象。 之后的为选项。 -fg red 表示红色前景色。 -text "hi there" 表示button显示的内容为 "hi there"

    3. Tk to python tkinter

    https://docs.python.org/3.0/library/tkinter.html#mapping-basic-tk-into-tkinter

    Mapping Basic Tk into Tkinter

    创建对象 button .fred                =====>  fred = Button()

    指定父对象 Tk对象指定其父对象:在创建时, 由路径名隐式指定。 Tkinter对象指定其父对象: 在创建时, 由类构造函数的第一个参数显式指定。 button .panel.fred          =====>  fred = Button(panel)

    Tk配置选项: 以连字符标记的列表形式给出,后面跟着值。 Tkinter配置选项: 在类构造函数中指定为关键字参数 button .fred -fg red        =====>  fred = Button(panel, fg="red")

    Tk更新配置选项: 使用 configure 命令, 后面以连字符标记的列表形式给出,后面跟着值。 Tkinter更新配置选项: 使用已建立的对象的索引或 使用对象的 configure 方法

    .fred configure -fg red     =====>  fred["fg"] = red                                  OR ==>  fred.config(fg="red")

    Tk widget产生一个动作: 使用widget的名字作为命令,后面是动作名,之后可以加选项. Tkinter widget产生一个动作:  使用widget的对象的动作方法 .fred invoke                =====>  fred.invoke()

    widget打包几何管理(geometry manager): Tk 打包: 使用pack命令, 后加路径,加选项 Tkinter打包: 封装了一个 Packer类, 所有的 widgets都是 Packer类的子类, widgets可以直接使用 pack方法。 See the tkinter.tix module documentation for additional information on the Form geometry manager. pack .fred -side left       =====>  fred.pack(side="left")

     

    Processed: 0.011, SQL: 9