python_Tkinter1
一个更详细的Tkinter笔记:
首先是最重要基础的,如何
创建一个窗口
窗口代码:
#coding:utf8
import Tkinter
show = Tkinter.Tk()
show.title("我的窗口")
show.geometry('150x150')
show.resizable(width=True,height=False)
# 进入消息循环
#print show.keys()
show.mainloop()
如图:

创建窗口的常用属性:
- title: 设置窗口标题
- geometry: 设置窗口大小
- resizable():设置窗口是否可以变化长、宽
代码中的print show.keys()输出窗口的基本属性:
['bd', 'borderwidth', 'class', 'menu', 'relief', 'screen', 'use',
'background', 'bg', 'colormap', 'container', 'cursor', 'height',
'highlightbackground', 'highlightcolor', 'highlightthickness', 'padx',
'pady', 'takefocus', 'visual', 'width']
可以像这样设置这些属性:
show['bg']='lightblue'
show['height']=100
Tkinter的其他控件:
Button
Label
Entry
Text
Checkbutton
Radiobutton
每个控件都要加上pack()才能显示!
Button
def say_hi():
print 'hello' b=tk.Button(text='hello',command=say_hi)
b.pack()
b1=tk.Button(text='quit',command=quit)
b1.pack()
如图:

第一个按钮,输出'hello'
第二个按钮关闭窗口
可以在代码中 print b.keys()查看Button的相关属性,并使用b['xxx']=xxx设置相关属性。
['activebackground', 'activeforeground', 'anchor', 'background', 'bd', 'bg', 'bitmap', 'borderwidth', 'command', 'compound', 'cursor', 'default', 'disabledforeground', 'fg', 'font', 'foreground', 'height', 'highlightbackground', 'highlightcolor', 'highlightthickness', 'image', 'justify', 'overrelief', 'padx', 'pady', 'relief', 'repeatdelay', 'repeatinterval', 'state', 'takefocus', 'text', 'textvariable', 'underline', 'width', 'wraplength']
Label&&Text
label:标签控件;可以显示文本和位图。
Text:文本控件;用于显示多行文本。
l=tk.Label(text='hello')
l['bg']='red'
l['foreground']='blue'
l['font']=15
l.pack()
t=tk.Text(width=100,height=100)
t.insert(1.0,'insert into text')
t.pack()
text中还可以插入按钮,插入图片,示例:
#coding:utf8
from Tkinter import * show = Tk()
text = Text(show,width=30,height=15)
#text插入按钮
b1 = Button(text,text='click me',command=quit)
text.window_create(INSERT,window=b1)
#text插入图片
img = PhotoImage(file='beauty.gif')
text.image_create(END,image=img)
text.pack()
mainloop()

这里想说一下,img = PhotoImage(file='beauty.gif')中只能是gif,jpg图片不行,会报错。
(但是你gif运行出来也不会动啊喂!)
Entry
Entry 文本输入框
还是老样子
e=tk.Entry()
e.insert(1,'哈哈哈哈嚯')#插入数据
e.pack()
怎么获得文本框的值:
按Enter键,输出Entry的值。
#coding:utf8
from Tkinter import *
def show(event=None):
print (e.get())
root = Tk()
u=StringVar()
e=Entry(root,textvariable=u)
e.pack()
e.bind('<Button>',show)
root.mainloop()
Radiobutton单选框&&Checkbox多选框
直接贴代码:
单选框点击选择哪个选项,那么就在上方label显示你的选项。
多选框点击选择哪个选项,那么就在下方label显示你的选项。
#coding:utf8
from Tkinter import *
import tkMessageBox
def show(event=None):
str="you select "+u.get()
#l['text']=str label没有set方法,可以这样设置内容
l.config(text=str)
def show1(event=None):
list=["like"]
if a.get()==True:
list.append("bird")
elif "bird" in list:
list.remove("bird")
if b.get()==True:
list.append("rabbit")
elif "rabbit" in list:
list.remove("rabbit")
if c.get()==True:
list.append("tiger")
elif "tiger" in list:
list.remove("tiger")
print list
clabel['text']=list
root = Tk()
#单选框
l=Label(root,text="you select read book")
l.pack()
u=StringVar()
u.set("no")
rb1=Radiobutton(root,text='book',variable=u,value="read book",command=show)
rb2=Radiobutton(root,text='movie',variable=u,value="watch movie",command=show)
rb3=Radiobutton(root,text='game',variable=u,value="play games",command=show)
rb1.select() #不写这句的话,默认初始状态三个都被勾选
rb1.pack()
rb2.pack()
rb3.pack()
a=BooleanVar()
b=BooleanVar()
c=BooleanVar()
#复选框
cb1=Checkbutton(root,text="bird",variable=a,command=show1)
cb2=Checkbutton(root,text="rabbit",variable=b,command=show1)
cb3=Checkbutton(root,text="tiger",variable=c,command=show1)
cb1.pack()
cb2.pack()
cb3.pack()
clabel=Label(root,text="")
clabel.pack()
root.mainloop()
贴图:

python_Tkinter1的更多相关文章
随机推荐
- nginx1.14.0版本https加密配置
修改host文件,为最后访问域名准备 C:\Windows\System32\drivers\etc host文件目录192.168.10.140 www.joyce.com 在最后添加这个自定义域名 ...
- Kong(V1.0.2) Securing the Admin API
Introduction Kong的Admin API为Services, Routes, Plugins, Consumers, and Credentials的管理和配置提供了一个RESTful接 ...
- spring boot profiles 实现多环境下配置切换 docker版
1,前言 开发环境总需要调试,docker直接部署不需要调试,环境配置不一样,这里的目的只是,在docker文件环境与开发环境使用不同的配置文件,项目结构如下 2,设置项目配置文件 默认配置文件 ap ...
- spark-rpc是如何实现将netty的Channel隐藏在inbox中的
class TransportServer bootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Overri ...
- SpringMVC Controller接收前台ajax的GET或POST请求返回各种参数
这几天写新项目遇到这个问题,看这位博主总结得不错,懒得写了,直接转!原文:http://blog.csdn.net/yixiaoping/article/details/45281721原文有些小错误 ...
- Spark下的FP-Growth和Apriori
基本概念 关联分析是一种在大规模数据集中寻找有趣关系的非监督学习算法.这些关系可以有两种形式:频繁项集或者关联规则.频繁项集(frequent item sets)是经常出现在一块的物品的集合,关联规 ...
- Win 10更新版1709有哪些新功能值得关注!
windows 10秋季创意者更新版1709发布已经有段时间了,也有很多用户选择升级这次更新的系统.那么,这次Win 10 更新版1709有哪些新功能值得关注呢?下面,一起随主机吧来看一看吧! 1. ...
- django框架使用mysql报错,及两种解决方法
1.django框架 settings.py文件中部分代码: DATABASES = { # 'default': { # 'ENGINE': 'django.db.backends.sqlite3' ...
- PHP语言入门的学习方法十要素
对于PHP程序设计语言来说.每个人的学习方式不同,写这篇文章的目的是分享一下自己的学习过程,仅供参考,不要一味的用别人的学习方法,找对自己有用的学习方式.经常在某些论坛和QQ群里看到一些朋友会问“怎样 ...
- STS临时授权访问OSS
STS临时授权访问OSS OSS 可以通过阿里云 STS (Security Token Service) 进行临时授权访问.阿里云 STS 是为云计算用户提供临时访问令牌的Web服务.通过 STS, ...