打印
[嵌入式linux]

记录下用python tk做界面的知识 (windows GUI )

[复制链接]
7528|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
sinanjj|  楼主 | 2010-11-25 22:04 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
沙发
sinanjj|  楼主 | 2010-11-25 22:05 | 只看该作者
本帖最后由 sinanjj 于 2010-11-25 22:16 编辑

简介:
这个是TK8.5教程.
劳资用TK15年了, 新手高手都可以看下这个教程, 有利无害(废话)
TK的开放文档确实有点跟不上时代.

这篇教程针对三大主流平台indows, Mac, Linux. 绑定 Tcl, Ruby, Perl 和Python语言.

这篇教程针对谁
应用层的用户.


(下边那些直接跳过.)

使用特权

评论回复
板凳
sinanjj|  楼主 | 2010-11-25 22:17 | 只看该作者
本帖最后由 sinanjj 于 2010-11-25 22:21 编辑

http://www.tkdocs.com/tutorial/install.html

Installing Tk

Though there are lots of ways to install Tk, the easiest is to download and install one of the versions provided by ActiveState (www.activestate.com).

windows上装tk, python
python已经自带.
测试命令:
>>> import tkinter
>>> tkinter._test()

第一个程序"Hello World"
编辑hello.py文件:
from tkinter import *
from tkinter import ttk
root = Tk()
button = ttk.Button(root, text="Hello World").grid()
root.mainloop()

        dos shell内运行
>python hello.py

使用特权

评论回复
地板
sinanjj|  楼主 | 2010-11-25 22:55 | 只看该作者
本帖最后由 sinanjj 于 2010-11-26 09:47 编辑

http://www.tkdocs.com/tutorial/firstexample.html



一个真实的例子


画个草图:




我们有一个按钮, 一个输入框, 3个lable

分割如下:


代码:


from tkinter import *
from tkinter import ttk

def calculate(*args):
    try:
        value = float(feet.get())
        meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
    except ValueError:
        pass


root = Tk()
root.title("Feet to Meters")

mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)

feet = StringVar()
meters = StringVar()

feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))

ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)

ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)

feet_entry.focus()
root.bind('<Return>', calculate)

root.mainloop()



一步一步的解释下


from tkinter import *
from tkinter import ttk
导入库.

root = Tk()
root.title("Feet to Meters")
mainframe = ttk.Frame(root, padding="3 3 12 12")
mainframe.grid(column=0, row=0, sticky=(N, W, E, S))
mainframe.columnconfigure(0, weight=1)
mainframe.rowconfigure(0, weight=1)        
先跳开calculate函数, 因为那还没被调用.


以上的这些代码设置主窗口, 设置标题为 "Feet to Meters". 然后我们创建一个frame控件, 并把它放到主窗口.  "columnconfigure"/"rowconfigure" 告诉主窗口如果从拉伸就要自己从新设置.



feet = StringVar()
meters = StringVar()
feet_entry = ttk.Entry(mainframe, width=7, textvariable=feet)
feet_entry.grid(column=2, row=1, sticky=(W, E))
ttk.Label(mainframe, textvariable=meters).grid(column=2, row=2, sticky=(W, E))
ttk.Button(mainframe, text="Calculate", command=calculate).grid(column=3, row=3, sticky=W)

上边的代码创建了3个控件: 一个输入框,一个lable, 一个按钮.

ttk.Label(mainframe, text="feet").grid(column=3, row=1, sticky=W)
ttk.Label(mainframe, text="is equivalent to").grid(column=1, row=2, sticky=E)
ttk.Label(mainframe, text="meters").grid(column=3, row=2, sticky=W)

创建并放置3个label

for child in mainframe.winfo_children(): child.grid_configure(padx=5, pady=5)
feet_entry.focus()
root.bind('<Return>', calculate)

The preceeding three lines help put some nice finishing touches on our user interface.
The first line walks through all of the widgets that are children of our content frame,and adds a little bit of padding around each, so they aren't so scrunched together.We could have added these options to each "grid" call when we first put the widgetsonscreen, but this is a nice shortcut.The second line tells Tk to put the focus on our entry widget.  That way the cursor willstart in that field, so the user doesn't have to click in it before starting to type.
The third line tells Tk that if the user presses the Return key (Enter on Windows) anywherewithin the root window, that it should call our calculate routine, the same as if theuser pressed the Calculate button.



def calculate(*args):
    try:
        value = float(feet.get())
        meters.set((0.3048 * value * 10000.0 + 0.5)/10000.0)
    except ValueError:
        pass

Here we define our calculate procedure, which is called either when the user pressesthe Calculate button, or hits the Return key.  It performs the feet to meterscalculation, taking the number of feet from our entry widget, and placing the resultin our label widget.
Say what?  It doesn't look like we're doing anything with those widgets!  Here's wherethe magic "textvariable" options we specified when creating the widgets come into play.  We specified the global variable "feet" as the textvariable for the entry, which meansthat anytime the entry changes, Tk will automatically update the global variable feet.Similarly, if we explicitly change the value of a textvariable associated with a widget(as we're doing for "meters" which is attached to our label), the widget will automaticallybe updated with the current contents of the variable.  Slick.



root.mainloop();This final line tells Tk to enter its event loop, which is needed to make everything run.

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:In God We Trust 独立的个人,体赖科学技术工具提供针对个人的产品与服务,是通向幸福的唯一道路 工程师,设计师等可以个人创业的群体,将逐步瓦解官僚体制公司,成为中国中产。(重复劳动,工厂等,将逐步机械化) seacer.co

456

主题

6300

帖子

25

粉丝