- 論壇徽章:
- 0
|
第一個(gè):最簡單的也是最經(jīng)典的程序helloworld
1 #!/usr/bin/env python
2 import Tkinter#導(dǎo)入tk庫
3 top=Tkinter.Tk()#建立主窗口
4 label=Tkinter.Label(top,text='hello world!!')#標(biāo)簽組件
5 label.pack()#封裝標(biāo)簽
6 Tkinter.mainloop()#事件循環(huán)
第二個(gè):設(shè)置背景及字體顏色
1 #!/usr/bin/env python
2 import Tkinter#導(dǎo)入tk庫
3 top=Tkinter.Tk()#建立主窗口
4 label=Tkinter.Label(top,text='hello world!!',bg=’red’,fg=’green’)#標(biāo)簽組件,bg背景色,fg字體顏色
5 label.pack()#封裝標(biāo)簽
6 Tkinter.mainloop()#事件循環(huán)
第三個(gè):加入按鈕組件,并將背景色設(shè)置為黑色。
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()
第四個(gè):應(yīng)用框架
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')#鼠標(biāo)未放上之前白色,放上之后紅色
21 quit.pack(fill=Tkinter.X, expand=1)
22
23 Tkinter.mainloop()
本文來自ChinaUnix博客,如果查看原文請(qǐng)點(diǎn):http://blog.chinaunix.net/u2/73535/showart_1423157.html |
|