Tkinter入门

参考,翻译,转述 自:http://www.pythonware.com/library/tkinter/introduction/index.htm

Tkinter是一种python的编程接口,基于tk库,而tk是一组GUI toolkits,解决了跨平台UI显示。 简单说,也就是我们可以使用Tkinter和Python来编写图形应用程序员。使用Tkinter十分的简单,只要在Python中导入该模块就可以使 用了。

【Example 1】下面是最小化的hello world的一个示例:

 1 # File: hello1.py
 2 
 3 from Tkinter import * #导入模块
 4 
 5 root = Tk() #实例化根节点
 6 
 7 w = Label(root, text="Hello, world!") #实例化一个子节点label
 8 w.pack() #对label进行大小和可见性的相关设置
 9 
10 root.mainloop() #启动应用程序事件循环
运行,显示一个标签的窗口,窗口标题为Tk.


【Example 2】下面,使用类来对子控件的创建进行封装:

# File: hello2.py

from Tkinter import *

# 将子控件打包在App类,并绑定到相同的成员方法
class App:

    def __init__(self, master):

        frame = Frame(master)
        frame.pack()

        self.button = Button(frame, text="QUIT", fg="red", command=frame.quit)
        self.button.pack(side=LEFT)

        self.hi_there = Button(frame, text="Hello", command=self.say_hi)
        self.hi_there.pack(side=LEFT)

    def say_hi(self):
        print "hi there, everyone!"

root = Tk()

app = App(root) #实例化时传入root节点

root.mainloop() #同样启动事件循环

 

转载于:https://www.cnblogs.com/nirvana-phoenix/archive/2012/04/24/2467975.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值