时间:2020-07-27 python教程 查看: 967
本文实例讲述了Python GUI编程学习笔记之tkinter界面布局显示。分享给大家供大家参考,具体如下:
首发时间:2018-03-04 14:20
from tkinter import *
root=Tk()
# for i in range(10):
##side
# btn = Button(text=i)
# btn.pack(side=LEFT)
# btn.pack(side=RIGHT)
# btn.pack(side=(TOP,LEFT))
####fill
# btn.pack(fill=X)
# btn.pack(side=LEFT,fill=Y)
# btn.pack(fill=BOTH)
##expand
# btn1=Button(text=1,bg='green')
# btn2=Button(text=2,bg='yellow')
# btn1.pack(fill=X)
# btn2.pack(expand=True)
#anchor:
# btn1=Button(text=1,bg='green')
# btn2=Button(text=2,bg='yellow')
# btn1.pack(fill=X)
# btn2.pack(anchor=E)
#pack_propagete
# btn1=Button(text=1,bg='green')
# btn2=Button(text=2,bg='yellow')
# root.pack_propagate(0)
# btn1.pack()
# btn2.pack()
#pack_forget:
# import time,threading
# label=Label(text="警告!")
# label.pack()
# def run():
# start_time=time.time()
# while True:
# if time.time()-start_time>3:
# label.pack_forget()
# break
# t=threading.Thread(target=run)
# t.setDaemon(True)
# t.start()
root.mainloop()
grid_propagete
:由窗口调用来配置pack控件时窗口的大小,当flag=0时,那么窗口大小将不再仅仅包裹住所有控件,这时候窗口的高度和宽度设置才可以生效grid_slaves():以列表方式返回本组件的所有子组件对象。
from tkinter import *
root=Tk()
root.title("我的窗口")
label_user=Label(text='用户名:',bg='green')
label_pwd=Label(text='密码:',bg='green')
user=Entry()
pwd=Entry()
# row,column,sticky
label_user.grid(row=0,column=0,sticky=W) #一个有sticky,一个没有sticky,以作区分
label_pwd.grid(row=1,column=0)
# rowspan,columnspan
user.grid(row=0,column=1)
pwd.grid(row=1,column=1)
btn=Button(text="提交")
btn.grid(row=0,column=3,rowspan=2,columnspan=2,padx=5, pady=5)
# 下面主要是将第一列拉大来显示上面sticky的效果
v=IntVar()
check=Checkbutton(text="下次自动登录",variable=v,)
check.grid(row=2,column=0,sticky=W)
# _forget:
import time,threading
label=Label(text="欢迎登录!",bg='blue')
label.grid()
def run():
start_time=time.time()
while True:
if time.time()-start_time>3:
label.grid_forget()
break
t=threading.Thread(target=run)
t.setDaemon(True)
t.start()
root.mainloop()
from tkinter import *
root=Tk()
root.geometry('100x100')
label=Label(text="用户名:",bg='green')
entry=Entry()
label.pack(fill=X)
#x,y
# entry.place(x=0,y=0)
#relx,rely
# entry.place(relx=0.3, rely=0.3)
# entry.place(relx=0.5, rely=0.5)
#relheight,relwidth
# entry.place(relx=0.5, rely=0.5,relheight=0.5,relwidth=0.5)
#_forget:
import time,threading
label2=Label(text="警告!",bg='blue')
label2.place(relx=0.2,rely=0.2)
def run():
start_time=time.time()
while True:
if time.time()-start_time>3:
label2.place_forget()
break
t=threading.Thread(target=run)
t.setDaemon(True)
t.start()
root.mainloop()
想要了解更多,可以参考tkinter的官方文档:http://effbot.org/tkinterbook/
希望本文所述对大家Python程序设计有所帮助。