python之GUI自定义界面设计 2014-4-10

#自定义界面设计
mybutton = Button(parent, **configuration options)
也可以这么写
mybutton.configure(**options)
颜色可以用rgb 也可以使用颜色标准名或者预定义颜色表示
可在 C:\Python27\Tools\pynche html40colors.txt 看到
# HTML 4.0 color names
Black #000000
Silver #c0c0c0
Gray #808080
White #ffffff
Maroon #800000
Red #ff0000
Purple #800080
Fuchsia #ff00ff
Green #008000
Lime #00ff00
Olive #808000
Yellow #ffff00
Navy #000080
Blue #0000ff
Teal #008080
Aqua #00ffff
字体 样式设置
widget.configure( font= ‘font family, fontsize, optional style modifiers
like bold, italic, underline and overstrike‘)
例子
widget.configure (font=‘Times, 8‘)
widget.configure (font = ‘Helvetica 24 bold italic‘)
大小参数 可以使用px 也可以使用m(millimiters),c(centimeters),i(inches)等
Tkinter的边框一般是2px 可以修改 如下:
button.configure(borderwidth=5)
widget有5种浮雕样式:
flat, raised, sunken,groove, an d ridge.
设置如下
button.configure (relief=‘raised‘)

鼠标(悬浮状态)样式也可以变化
button.configure (cursor=‘cross‘)

为方便自定义样式 可以将样式写在一个txt文件 作为样式数据
*font: Arial 10
*Label*font: Times 12 bold
*background: AntiqueWhite1
*Text*background: #454545
*Button*foreground:gray55
*Button*relief: raised
*Button*width: 3
加*表示 通用于widget的所有元素
root.option_readfile(‘optionDB.txt‘)
option_readfile表示调用哪个数据

案例:
root.title("title of my program")
定义标题
root.geometry(‘142x280+150+200‘)
定义尺寸和坐标 widthxheight + xoffset + yoffset

self.root.wm_iconbitmap(‘mynewicon.ico‘)
or
self.root.iconbitmap(‘mynewicon.ico ‘)
修改默认图标
root.overrideredirect(1)
移除框架

案例:
from Tkinter import *
root = Tk()
#demo of some important root methods
root.geometry(‘142x280+150+200‘) #specify root window size and position
root.title("Style Demo") #specifying title of the program
#root.wm_iconbitmap(‘brush1.ico‘)#changing the default icon
#root.overrideredirect(1) # remove the root border - uncomment
#this line to see the difference
#root.configure(background=‘#4D4D4D‘)#top level styling
# connecting to the external styling optionDB.txt
#root.option_readfile(‘optionDB.txt‘)
#widget specific styling
mytext = Text(root, background=‘#101010‘, foreground="#D6D6D6",
borderwidth=18, relief=‘sunken‘, width=16, height=5 )
mytext.insert(END, "Style is knowing \nwho you are, what \nyou want to say, \nand not giving a \ndamn.")
mytext.grid(row=0, column=0, columnspan=6, padx=5, pady=5)
# all the below widgets derive their styling from optionDB.txt file
Button(root, text=‘*‘ ).grid(row=1, column=1)
Button(root, text=‘^‘ ).grid(row=1, column=2)
Button(root, text=‘#‘ ).grid(row=1, column=3)
Button(root, text=‘<‘ ).grid(row=2, column=1)
Button(root, text=‘OK‘, cursor=‘target‘).grid(row=2, column=2)
Button(root, text=‘>‘).grid(row=2, column=3)
Button(root, text=‘+‘ ).grid(row=3, column=1)
Button(root, text=‘v‘, font=‘Verdana 8‘).grid(row=3, column=2)
Button(root, text=‘-‘ ).grid(row=3, column=3)
for i in range(0,10,1):
Button(root, text=str(i) ).grid( column=3 if i%3==0 else (1 if i%3==1
else 2), row= 4 if i<=3 else (5 if i<=6 else 6))
#styling with built-in bitmap images
mybitmaps = [‘info‘, ‘error‘, ‘hourglass‘, ‘questhead‘, ‘question‘,
‘warning‘]
for i in mybitmaps:
Button(root, bitmap=i, width=20,height=20).grid(row=
(mybitmaps.index(i)+1), column=4,sticky=‘nw‘)
root.mainloop()

python之GUI自定义界面设计 2014-4-10,布布扣,bubuko.com

python之GUI自定义界面设计 2014-4-10

上一篇:java 单例模式


下一篇:c++中"endl"和"\n"的区别