Python GUI编程--Tkinter
今天看到了GUI编程,书上推荐用wxPython,去官网上看了看,发现Windows的最高支持到2.7,我用的是3.4版本,咋办,用自带的库--Tkinter呗,它是Python的默认GUI库,几乎是个标准库,也是受广大开发者喜爱的。
Tkinter有很多组件(其实也不多,十来个),今天主要用标签、按钮、进度条,写一个字体随进度条改变大小并且可以写文件的小程序,其他组件用法看文档就行,以前用C#写过winform的应该有经验。直接上代码:
#---coding:utf-8---
from tkinter import * #导包
def resize(ev=None):
'根据进度条调整字体大小'
label.config(font='Helvetica -%d bold' %scale.get()) def writefile():
'写文件'
try:
f = open(r'd:\hello.txt','w')
f.write('hello,world!')
except Exception as e:
print(e)
finally:
f.close() top = Tk()#新建一个窗口
top.geometry('400x300')#指定窗口大小
top.title('GUI_test') label = Label(top,text='Hello,World!',font='Helvetica -12 bold')#随进度条变化的标签,刚开始学当然用hello,world
label.pack(fill=Y,expand=1) scale = Scale(top,from_=10,to=50,orient=HORIZONTAL,command=resize)#进度条,个人认为command作用和绑定差不多
scale.set(12)#设初值
scale.pack(fill=X,expand=1) write = Button(top,text="Write",command=writefile)
write.pack() quit = Button(top,text="Quit",command=top.quit,activeforeground='White',
activebackground='red')
quit.pack() mainloop()#调用该函数运行程序
注意到每配置好一个组件,后面都有一句X.pack(***),最后的mainloop()是用来启动你编的GUI程序,那pack()是什么鬼,查一下文档,有这么一段话
The packer is one of Tk’s geometry-management mechanisms. Geometry managers are used to specify the relative positioning of the positioning of widgets within their container - their mutual master. In contrast to the more cumbersome placer (which is used less commonly, and we do not cover here), the packer takes qualitative relationship specification - above, to the left of, filling, etc - and works everything out to determine the exact placement coordinates for you.The pack() method can be called with keyword-option/value pairs that control where the widget is to appear within its container, and how it is to behave when the main application window is resized.意思大概就是说packer这哥们是管理和显示组件的,pack()方法用来指定组件的显示。看下运行效果:
打开D盘hello.txt,发现了我们写的hello,world!这样简单的GUI编程完成了,还用到了一点文件编程的知识,和预期效果一样,得赶快学习网络部分了,不然毕设进度得耽搁了。
Python GUI编程--Tkinter的更多相关文章
- Python GUI编程(Tkinter) windows界面开发
Python实现GUI简单的来说可以调用Tkinter库,这样一般的需求都可以实现,显示简单的windows窗口代码如下: python_gui.py 1 #!C:\Python27\python.e ...
- python GUI编程tkinter示例之目录树遍历工具
摘录 python核心编程 本节我们将展示一个中级的tkinter应用实例,这个应用是一个目录树遍历工具:它会从当前目录开始,提供一个文件列表,双击列表中任意的其他目录,就会使得工具切换到新目录中,用 ...
- Python -- Gui编程 -- Tkinter的使用 -- 基本控件
1.按钮 tkBtton.py import tkinter root = tkinter.Tk() btn1 = tkinter.Button(root, anchor=tkinter.E,\ te ...
- Python GUI编程(Tkinter)(一)
tk官网的教程学习: https://tkdocs.com/tutorial/firstexample.html 学习blog: https://www.cnblogs.com/aland-1415/ ...
- Python -- Gui编程 -- Tkinter的使用 -- 对话框消息框
1.消息框 tkMessageBox.py import tkinter from tkinter import messagebox def cmd(): global n global butto ...
- Python GUI编程(Tkinter)Ⅱ
Tkinter 组件 Tkinter的提供各种控件,如按钮,标签和文本框,一个GUI应用程序中使用.这些控件通常被称为控件或者部件. 目前有15种Tkinter的部件.我们提出这些部件以及一个简短的介 ...
- Python -- Gui编程 -- Tkinter的使用 -- 菜单与画布
1.菜单 tkMenu.py import tkinter root = tkinter.Tk() menu = tkinter.Menu(root) submenu = tkinter.Menu(m ...
- python大法好——ython GUI编程(Tkinter)
Python GUI编程(Tkinter) Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的 ...
- Python之GUI编程(Tkinter))
不足之处,还请海涵,请指出不足.本人发布过的文章,会不断更改,力求减少错误信息. 一.重要放在开头:模块 如出现这种错误 ModuleNotFoundError: No module named 'n ...
随机推荐
- win7 下加载MSCOMCTL.OCX
cd C:\Windows\System32"C:\Windows\Microsoft.NET\Framework64\v4.0.30319\regtlibv12.exe" msd ...
- HTML5新增标签(新增27个标签,废弃16个标签)
1>结构性标签:负责web上下文结构的定义 article:文章主题内容 header:标记头部区域内容 footer:标记脚部区域内容 section:区域章节表述 nav:菜单导航,链接导航 ...
- 初学JQuery笔记
extend()函数是jQuery的基础函数之一,作用是扩展现有的对象 <script type="text/javascript" src="jquery-1.5 ...
- shell 里的变量 总结
对于linux shell的使用者来说, 巧妙的应用变量不仅能够快速的解决问题,同时能够获取非常大的乐趣,因为shell的变量内部可以附加一些运算,使得程序非常简洁明了并且功能强大,以下详细介绍一下: ...
- 机器学习--Classifier comparison
最近在学习机器学习,学习和积累和一些关于机器学习的算法,今天介绍一种机器学习里面各种分类算法的比较 #!/usr/bin/python # -*- coding: utf-8 -*- "&q ...
- scala 学习之:list span 用法
Pack consecutive duplicates of list elements into sublists. If a list contains repeated elements the ...
- iOS中几种常用的数据存储方式
自己稍微总结了一下下,方便大家查看 1.write直接写入文件的方法 永久保存在磁盘中,可以存储的对象有NSString.NSArray.NSDictionary.NSData.NSNumber,数据 ...
- myeclipse部署web项目到server : deploy location 为 INVALID,并且不能更改
首先查看项目路径下有没有 .mymetadata文件,没有就从其它地方挪一个,但是文件中的id必须是唯一的. 如果还不行就去下面这位大哥写的博客看下,进行下一步就可以解决了 生如夏花
- notepad++的CoolFormat代码格式化插件使用
因为notepad++的NppAStyle插件只支持格式化C.C++.C#.Java这四种编程语言的代码,所以本人推荐使用这个CoolFormat的插件,相比于NPPAStyle,CoolFormat ...
- mouseChildren启示
将aSprite的 mouseChildren 属性设置为 false ,可以实现mouseClick 事件的目标为 aSprite对象,而不是其子对象中的任一个.