import Tkinter as tk
app=tk.Tk()##创建Tk类
app.title("YUE~AN")##标题
thelabel=tk.Label(app,text="YUE AN")##标签,上面可以添加文本,图像等
thelabel.pack()##可以自动调节大小的方法
app.mainloop()
import Tkinter as tk
class APP:
def __init__(self,name):
##做2个框架。
frame=[]
for i in range(2):
frame.append(tk.Frame(name))
frame[0].pack(side=tk.LEFT,padx=10,pady=3)
name.title("yue")
self.hi_there = tk.Button(frame[0],text="say hi~",bg="black",fg="white",command=self.say_hi)
self.hi_there.pack()
##还不能用~
self.label=tk.Label(name,text="lalala!")
self.label.pack()
def say_hi(self):
print("hello ~ !")
root = tk.Tk()
app = APP(root)
root.mainloop()
pack
我们使用 pack 函数的时候,默认先使用的放到上面,然 后 依次向下排,它会给我们的组件一个自认为合适的位置 和大小,这是默认方式。
每设置一次按钮,框架,标签后,即可通过pack方法来调整它们的位置。
引用块内容
pack布局非常简单,不用做过多的设置,直接使用一个 pack 函数就可以了。
可接受的参数:side:停靠在哪个方向
left: 左
top: 上
right: 右
botton: 下
fill:填充
x:水平方向填充
y:竖直方向填充
both:水平和竖直方向填充
none:不填充
expand:
yes:
no:
anchor:
N:北 下
E:东 右
S:南 下
W:西 左
CENTER:中间
padx:x方向的外边距
pady:y方向的外边距
ipadx:x方向的内边距
ipady:y方向的内边距
from Tkinter import *
root = Tk()
Button(root,text=’A’).pack(side=LEFT,expand=YES,fill=Y)
Button(root,text=’B’).pack(side=TOP,expand=YES,fill=BOTH)
Button(root,text=’C’).pack(side=RIGHT,expand=YES,fill=NONE)
Button(root,text=’D’).pack(side=LEFT,expand=NO,fill=Y)
Button(root,text=’E’).pack(side=TOP,expand=YES,fill=BOTH)
Button(root,text=’F’).pack(side=BOTTOM,expand=YES)
Button(root,text=’G’).pack(anchor=SE)
root.mainloop()
关于frame
from Tkinter import *
root = Tk()
#以不同的颜色区别各个frame
for fm in ['red','blue','yellow','green','white','black']:
#注意这个创建Frame的方法与其它创建控件的方法不同,第一个参数不是root
Frame(height = 20,width = 400,bg = fm).pack()
root.mainloop()
from Tkinter import *
def say_hi():
print("hello ~ !")
root = Tk()
frame1 = Frame(root)
frame2 = Frame(root)
root.title("yue")
label= Label(frame1,text="lalalalalallalalalalllalal!",justify=LEFT)
label.pack(side=LEFT)
hi_there = Button(frame2,text="say hi~",command=say_hi)
hi_there.pack()
frame1.pack(padx=1,pady=1)
frame2.pack(padx=10,pady=10)
root.mainloop()
似乎当只存在一个标签或者button时候,不存在side?
from tkinter import *
# 导入tkinter模块的所有内容
root = Tk()
# 创建一个文本Label对象
textLabel = Label(root,
text="lalalal,\nlalalala!",
justify=LEFT,#文字左对齐
padx=10)
textLabel.pack(side=LEFT)
# 创建一个图像Label对象
# 用PhotoImage实例化一个图片对象(支持gif格式的图片)
photo = PhotoImage(file="18.gif")
imgLabel = Label(root, image=photo)
imgLabel.pack(side=RIGHT)
mainloop()
from tkinter import *
root = Tk()
photo = PhotoImage(file="bg.gif")
theLabel = Label(root,
text="lalla\nlalal",
justify=LEFT,
image=photo,
compound=CENTER,##表示文字覆盖在图像上
font=("宋体", 20),
fg="white"
)
theLabel.pack()
mainloop()
关于label
http://blog.csdn.net/red_sola/article/details/41950975
from Tkinter import *
def callback():
var.set("aaaaaaa")
root=Tk()
var = StringVar()#创建可以更改内容的对象
var.set("bbbbbb")
textLabel = Label(root,
textvariable=var,
justify=LEFT)
textLabel.pack(side=LEFT)
but=Button(root,text= u'变',command=callback)
but.pack()
mainloop()
from Tkinter import *
def callback():
var1.set("Noo~ these change !")
root = Tk()
frame1 = Frame(root)
frame2 = Frame(root)
#可变字符串
var1 = StringVar()
var1.set("If you push the button\nthese contents could change !")
#背景图片 只支持gif
image = PhotoImage(file='bg.gif')
label = Label(frame1,
textvariable=var1,
justify=LEFT,
image = image,
compound=CENTER,
font=20,
fg="white"
)
label.pack()
frame1.pack()
button = Button(frame2,
text="broken English !",
command = callback)
button.pack(side=LEFT,padx=10,pady=10)
image2 = PhotoImage(file="18.gif")
label2 = Label(frame2,
image= image2,
compound=CENTER,
)
label2.pack()
frame2.pack()
mainloop()