pythongui实例⼤全_python图形GUI基本⽰例
【注:以下⽰例python3版本】
⽰例⼀:
importtkinterastk
app=()
("FishCDemo")
theLable=(app,text="我的第⼆个窗⼝程序",width=20,height=10)#标签控件;可以显⽰⽂本和位图
()
op()
运⾏结果:
⽰例⼆:
importtkinterastk
classApp:#定义类
def__init__(lf,master):
frame=(master)#定义窗体框架
(side=,padx=100,pady=100)
#定义按钮
_there=(frame,text="打招呼",bg="red",fg='green',command=_hi)
_(padx=10,pady=10)#位置
defsay_hi(lf):
print("Hi!⼤家好,终于我跟⼤家见⾯了。")
root=()
app=App(root)
op()
运⾏结果:
⽰例三
fromtkinterimport*
root=Tk()
textLabel=Label(root,text="有些⼼疼,⼜带着欣喜。n她那么简单,不好看,却努⼒的开着,
n为了迎合清风掠⼼的柔情,⼀直努⼒的开着,n半盏清歌的招摇,
肆意葱茏。n⽽我,恰巧路过。于是,n喜意便盈盈的⽣了起来,带
着异样的温暖和朴素,n将⼼,都染了湿湿的绿,盎然的,n万千端倪。",justify=LEFT,padx=10)#Label标签控件;可以显⽰⽂本和
位图
(side=LEFT)
photo=PhotoImage(format="png",file=r"C:UrsAdministratorDesktop123(2).png")#PhotoImagecanbeudforGIF
andPPM/PGMcolorbitmaps
imgLabel=Label(root,image=photo)
(side=RIGHT)
mainloop()
运⾏结果:
⽂字排版不啥样,只是展⽰效果⽽已,请原谅!
⽰例四
fromtkinterimport*
root=Tk()
photo=PhotoImage(file=r"C:")
theLable=Label(root,
text='Hello',
justify=LEFT,
image=photo,
compound=CENTER,
font=("⿊体",20),
fg="red")
()
mainloop()
运⾏结果:
展⽰效果⽽已!
⽰例五
fromtkinterimport*
defcallback():
('吹吧你,我才不信呢')
root=Tk()
frame1=Frame(root)#Frame框架控件;在屏幕上显⽰⼀个矩形区域,多⽤来作为容器
frame2=Frame(root)
var=StringVar()#设置字符串
("你所下载的影⽚含有末成⼈限制内容,n请满18周岁后再点击观看")
textLabel=Label(frame1,
textvariable=var,
justify=LEFT)
(side=LEFT)
photo=PhotoImage(file=r"C:UrsAdministratorDesktop123(2).png")
imgLabel=Label(frame1,text="图⽚⼀",image=photo)
(side=RIGHT)
theButton=Button(frame2,text="我已满18周岁",command=callback)#定义⼀个按钮
()
(padx=10,pady=10)#定义位置
(padx=50,pady=50)
mainloop()
运⾏结果
⽰例六
fromtkinterimport*
root=Tk()
frame=Frame(root)
v=IntVar()
c=Checkbutton(root,text="测试⼀下",variable=v)#定义⼀个复选框
()
l=Label(root,textvariable=v)
()
if__name__=='__main__':
mainloop()
运⾏结果:
⽰例七
fromtkinterimport*
root=Tk()
GIRLS=["西施","貂蝉","王昭君","杨⽟环"]
v=[]
forgirlinGIRLS:
(IntVar())
b=Checkbutton(root,text=girl,variable=v[-1])
(anchor=W)
mainloop()
运⾏结果:
⽰例⼋:
fromtkinterimport*
root=Tk()
v=IntVar()
Radiobutton(root,text="one",variable=v,value=1).pack(anchor=W)
Radiobutton(root,text="two",variable=v,value=2).pack(anchor=W)
Radiobutton(root,text="three",variable=v,value=3).pack(anchor=W)
mainloop()
运⾏结果:
⽰例九
fromtkinterimport*
root=Tk()
LANGES=[
("python",1),
("perl",2),
("Ruby",3),
("Lua",4)
]
v=IntVar()
(1)
forlang,numinLANGES:
b=Radiobutton(root,text=lang,variable=v,value=num,indicatoron=Fal)
(fill=X)
mainloop()
运⾏结果:
⽰例⼗:
fromtkinterimport*
root=Tk()
group=LabelFrame(root,text="最好的脚本语⾔是?",padx=5,pady=5)
(padx=10,pady=10)
LANGES=[
("python",1),
("perl",2),
("Ruby",3),
("Lua",4)
]
v=IntVar()
(1)
forlang,numinLANGES:
b=Radiobutton(group,text=lang,variable=v,value=num)
(anchor=W)
mainloop()
运⾏结果:
⽰例⼗⼀
fromtkinterimport*
root=Tk()
e=Entry(root)
(padx=20,pady=20)
(0,END)
(0,"默认⽂本...")
mainloop()
运⾏结果:
⽰例⼗⼆
fromtkinterimport*
root=Tk()
Label(root,text="作品:").grid(row=0,column=0)
Label(root,text="作者:").grid(row=1,column=0)
e1=Entry(root)
e2=Entry(root)
(row=0,column=1,pady=5)
(row=1,column=1,pady=5)
defshow():
print("作品:《%s》"%())
print("作者:《%s》"%())
Button(root,text="获取信息",width=10,command=show).grid(row=3,column=0,sticky=W,padx=10,pady=5)
Button(root,text="退出",width=10,command=).grid(row=3,column=1,sticky=W,padx=10,pady=5)
mainloop()
运⾏结果:
⽰例⼗三
fromtkinterimport*
root=Tk()
Label(root,text="账号:").grid(row=0,column=0)
Label(root,text="密码:").grid(row=1,column=0)
v1=StringVar()
v2=StringVar()
e1=Entry(root,textvariable=v1)
e2=Entry(root,textvariable=v2,show="*")
(row=0,column=1,pady=5)
(row=1,column=1,pady=5)
defshow():
print("账号:《%s》"%())
print("密码:《%s》"%())
Button(root,text="获取信息",width=10,command=show).grid(row=3,column=0,sticky=W,padx=10,pady=5)
Button(root,text="退出",width=10,command=).grid(row=3,column=1,sticky=W,padx=10,pady=5)
mainloop()
运⾏结果:
⽰例⼗四:
fromtkinterimport*
master=Tk()
deftest():
()=='cdx':
print("正确!")
returnTrue
el:
print("错误!")
(0,END)
returnFal
v=StringVar()
e1=Entry(master,textvariable=v,validate="focusout",validatecommand=test)
e2=Entry(master)
(padx=10,pady=10)
(padx=10,pady=10)
mainloop()
运⾏结果:
⽰例⼗五
fromtkinterimport*
master=Tk()
v=StringVar()
deftest1():
()=='cdx':
print("正确!")
returnTrue
el:
print("错误!")
(0,END)
returnFal
deftest2():
print("我被调⽤了..")
returnTrue
e1=Entry(master,textvariable=v,validate="focusout",validatecommand=test1,invalidcommand=test2)
e2=Entry(master)
(padx=10,pady=10)
(padx=10,pady=10)
mainloop()
运⾏结果:
⽰例⼗六
fromtkinterimport*
master=Tk()
v=StringVar()
deftest(content,reason,name):
ifcontent=="cdx":
print("正确!")
print(content,reason,name)
returnTrue
el:
print("错误!")
print(content,reason,name)
returnFal
testCMD=er(test)
e1=Entry(master,textvariable=v,validate="focusout",validatecommand=(testCMD,'%P','%v','%W'))
e2=Entry(master)
(padx=10,pady=10)
(padx=10,pady=10)
mainloop()
运⾏结果:
⽰例⼗七
fromtkinterimport*
master=Tk()
frame=Frame(master)
(padx=10,pady=10)
v1=StringVar()
v2=StringVar()
v3=StringVar()
deftest(content):
t()
testCMD=er(test)
e1=Entry(frame,width=10,textvariable=v1,validate="key",validatecommand=(testCMD,'%P')).grid(row=0,column=0)
Label(frame,text="+").grid(row=0,column=1)
e2=Entry(frame,width=10,textvariable=v2,validate="key",validatecommand=(testCMD,'%P')).grid(row=0,column=2)
Label(frame,text="+").grid(row=0,column=3)
e3=Entry(frame,width=10,textvariable=v3,state="readonly").grid(row=0,column=4)
defcalc():
result=int(())+int(())
(result)
Button(frame,text="计算结果",command=calc).grid(row=1,column=2,pady=5)
mainloop()
运⾏结果:
⽰例⼗⼋
fromtkinterimport*
master=Tk()
theLB=Listbox(master,lectmode=EXTENDED)#lectmode=SINGLE单选lectmode=EXTENDED多选
()
foritemin["鸡蛋","鸭蛋","鹅蛋","李狗蛋"]:
(END,item)
#(1)
theButton=Button(master,text='删除它',command=lambdax=theLB:(ACTIVE))
()
mainloop()
运⾏结果:
⽰例⼗九
fromtkinterimport*
master=Tk()
theLB=Listbox(master,lectmode=EXTENDED,height=8)#lectmode=SINGLE单选lectmode=EXTENDED多选
()
foriteminrange(11):
(END,item)
mainloop()
运⾏结果:
⽰例⼆⼗
fromtkinterimport*
root=Tk()
sb=Scrollbar(root)
(side=RIGHT,fill=Y)
lb=Listbox(root,yscrollcommand=)
foriinrange(1000):
(END,i)
(side=LEFT,fill=BOTH)
(command=)
mainloop()
运⾏结果:
本文发布于:2023-01-19 00:32:14,感谢您对本站的认可!
本文链接:http://www.wtabcd.cn/fanwen/fan/88/91404.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
留言与评论(共有 0 条评论) |