一个更详细的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的更多相关文章

随机推荐

  1. 理解OpenShift(5):从 Docker Volume 到 OpenShift Persistent Volume

    理解OpenShift(1):网络之 Router 和 Route 理解OpenShift(2):网络之 DNS(域名服务) 理解OpenShift(3):网络之 SDN 理解OpenShift(4) ...

  2. monodepth 训练记录

    2019年2月22日13:52:37 https://zhuanlan.zhihu.com/p/29968267 这里有个tensorlfow代码的阅读博客: https://zhuanlan.zhi ...

  3. LeetCode【101. 对称二叉树】

    对称二叉树,就是左节点的左节点等于右节点的右节点,左节点的右节点等于右节点的左节点. 很自然就想到迭代与递归,可以创建一个新的函数,就是另一个函数不断的判断,返回在主函数. class Solutio ...

  4. 知识点:Mysql 索引原理完全手册(2)

    知识点:Mysql 索引原理完全手册(1) 知识点:Mysql 索引原理完全手册(2) 知识点:Mysql 索引优化实战(3) 知识点:Mysql 数据库索引优化实战(4) 八. 联合索引与覆盖索引 ...

  5. vs关于“当前不会命中断点 还没有为该文档加载任何符号”的解决方法

    首先调式的时候确定在debug模式下, 解决方法:工具-选项-调试 -(启用“仅我的代码”)勾去掉.

  6. linux上安装mysql5.6

    CentOS-6.6+MySQL-5.6 部署环境操作系统:CentOS-6.6-x86_64-bin-DVD1.isoMySQL 版本:mysql-5.6.26.tar.gz操作用户:root系统 ...

  7. 爬虫——BeautifulSoup和Xpath

    爬虫我们大概可以分为三部分:爬取——>解析——>存储 一 Beautiful Soup: Beautiful Soup提供一些简单的.python式的函数用来处理导航.搜索.修改分析树等功 ...

  8. Docker笔记——jenkins镜像制作

    jenkins官方路径:https://hub.docker.com/_/jenkins/ 最新Dockerfile路径:https://github.com/jenkinsci/docker/blo ...

  9. [UNITY 5.4 UGUI] 模态对话框

    1.建立两个画布 a.背景界面 b.置顶界面(添加一个 panel 控件) 2.修改置顶界面中 panel ,添加属性 [Canvas Group] 3.根据界面设计情况修改透明度,色彩,图片

  10. .Net导出pdf文件,C#实现pdf导出 转载 http://www.cnblogs.com/hmYao/p/5842958.html

    导出pdf文件. 在编码前需要在网上下载个itextsharp.dll,此程序集是必备的.楼主下载的是5.0版本,之前下了个5.4的似乎不好用. 下载之后直接添加引用. <%@ Page Lan ...