Chinaunix首页 | 论坛 | 博客
  • 博客访问: 337661
  • 博文数量: 59
  • 博客积分: 2000
  • 博客等级: 大尉
  • 技术积分: 646
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-14 12:09
文章分类

全部博文(59)

文章存档

2009年(2)

2008年(57)

我的朋友

分类: Python/Ruby

2008-11-18 23:21:34

第一个:最简单的也是最经典的程序helloworld
 1 #!/usr/bin/env python
 2 import Tkinter#导入tk库
 3 top=Tkinter.Tk()#建立主窗口
 4 label=Tkinter.Label(top,text='hello world!!')#标签组件
 5 label.pack()#封装标签
 6 Tkinter.mainloop()#事件循环
第二个:设置背景及字体颜色
1 #!/usr/bin/env python
2 import Tkinter#导入tk库
3 top=Tkinter.Tk()#建立主窗口
4 label=Tkinter.Label(top,text='hello world!!',bg=’red’,fg=’green’)#标签组件,bg背景色,fg字体颜色
5 label.pack()#封装标签
6 Tkinter.mainloop()#事件循环
第三个:加入按钮组件,并将背景色设置为黑色。
  1 #!/usr/bin/env python
  2 import Tkinter
  3 top=Tkinter.Tk()
  4 quit=Tkinter.Button(top,text='hello world!!\n',command=top.quit,bg='black')
  5 quit.pack()
  6 label=Tkinter.Label(top,text='hello world!\n!')
  7 label.pack()
  8 Tkinter.mainloop()
第四个:应用框架
  1 import Tkinter
  2 from Tkconstants import *
  3
  4 top = Tkinter.Tk()
  5
  6 hello = Tkinter.Label(top, text='Hello World!',bg='green',fg='blue')
  7 hello.pack()
  8
  9 frame = Tkinter.Frame(top, relief=RIDGE, borderwidth=2,bg='yellow')
 10
 11 frame.pack(fill=BOTH,expand=1)
 12
 13 label = Tkinter.Label(frame, text="Hello, World",fg='green')
 14 label.pack(fill=X, expand=1)
 15
 16 button = Tkinter.Button(frame,text="Exit",command=top.destroy,bg='blue')
 17 button.pack(side=BOTTOM)
 18
 19
20quit=Tkinter.Button(top, text='QUIT',command=top.quit, activeforeground='w hite',activebackground='red')#鼠标未放上之前白色,放上之后红色
 21 quit.pack(fill=Tkinter.X, expand=1)#这里的X表示放大到最大后,填充X轴即横向,将其改为Y则为纵向
 22
 23 Tkinter.mainloop()
阅读(7559) | 评论(0) | 转发(1) |
给主人留下些什么吧!~~