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 ...
随机推荐
- 查询阻塞的sql
WITH ProcessCTE(blocked) AS ( SELECT blocked FROM sys.sysprocesses WHERE blocked>0 union SELECT s ...
- 循序渐进Python3(十一) --5-- 同源策略
一.什么是同源策略 同源策略(Same origin policy)是一种约定,它是浏览器最核心也最基本的安全功能.它是由Netscape提出的一个著名的安全策略,现在所有的可支持javascript ...
- linux组、用户操作相关
Linux删除用户组和用户时常用的一些命令和参数.1.从组中删除用户编辑/etc/group 找到GROUP1那一行,删除 A或者用命令gpasswd -d A GROUP2.建用户:adduser ...
- tomcat启动时 myeclipse控制台中文乱码
情况1: tomcat中conf目录下有个叫server.xml的文件,里面 <Connector port="8080" protocol="HTTP/1.1&q ...
- js escape
JS转义 escape().encodeURI().encodeURIComponent()区别详解 JavaScript中有三个可以对字符串编码的函数,分别是: escape,encodeURI,e ...
- [原创]Matlab之复选框使用
本文简单记录在Matlab的GUI设计中,复选框的一些使用,比较简单. 简单到直接上代码,就是可能比较容易忘记,使用的时候再翻回来好了. 1 2 3 4 5 6 7 % 复选框,选中后为1,未选中则为 ...
- Android发送短信核心代码
核心代码:(1)SmsManager manager = SmsManager.getDefault(); //获得默认的消息管理器(2)ArrayList<String> list = ...
- C++ Primer Plus 笔记记录
(一) /a 这个转移字符竟然能调用计算机的硬件 喇叭~~ 对于float c++只能保证6位有效数字 似乎 double是13位 cout.setf(ios_base::fixed, ios_bas ...
- highcharts曲线图
在做项目时,用highcharts做过曲线图,X轴是从后台获取的时间数据,Y轴是从后台获取的Int型数据 1.我的后台数据封装成json格式,数据较多,展示部分数据 2.曲线图的展示 3.前端jsp页 ...
- 自定义View的学习(一) 自绘制控件
一.自绘控件 就是自己绘制的控件,通过onDraw()方法将控件绘制出来 自定义一个可点击的View 这个View可以记住用户点击的次数 public class CounterView exte ...