今天看到了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的更多相关文章

  1. Python GUI编程(Tkinter) windows界面开发

    Python实现GUI简单的来说可以调用Tkinter库,这样一般的需求都可以实现,显示简单的windows窗口代码如下: python_gui.py 1 #!C:\Python27\python.e ...

  2. python GUI编程tkinter示例之目录树遍历工具

    摘录 python核心编程 本节我们将展示一个中级的tkinter应用实例,这个应用是一个目录树遍历工具:它会从当前目录开始,提供一个文件列表,双击列表中任意的其他目录,就会使得工具切换到新目录中,用 ...

  3. Python -- Gui编程 -- Tkinter的使用 -- 基本控件

    1.按钮 tkBtton.py import tkinter root = tkinter.Tk() btn1 = tkinter.Button(root, anchor=tkinter.E,\ te ...

  4. Python GUI编程(Tkinter)(一)

    tk官网的教程学习: https://tkdocs.com/tutorial/firstexample.html 学习blog: https://www.cnblogs.com/aland-1415/ ...

  5. Python -- Gui编程 -- Tkinter的使用 -- 对话框消息框

    1.消息框 tkMessageBox.py import tkinter from tkinter import messagebox def cmd(): global n global butto ...

  6. Python GUI编程(Tkinter)Ⅱ

    Tkinter 组件 Tkinter的提供各种控件,如按钮,标签和文本框,一个GUI应用程序中使用.这些控件通常被称为控件或者部件. 目前有15种Tkinter的部件.我们提出这些部件以及一个简短的介 ...

  7. Python -- Gui编程 -- Tkinter的使用 -- 菜单与画布

    1.菜单 tkMenu.py import tkinter root = tkinter.Tk() menu = tkinter.Menu(root) submenu = tkinter.Menu(m ...

  8. python大法好——ython GUI编程(Tkinter)

    Python GUI编程(Tkinter) Python 提供了多个图形开发界面的库,几个常用 Python GUI 库如下: Tkinter: Tkinter 模块(Tk 接口)是 Python 的 ...

  9. Python之GUI编程(Tkinter))

    不足之处,还请海涵,请指出不足.本人发布过的文章,会不断更改,力求减少错误信息. 一.重要放在开头:模块 如出现这种错误 ModuleNotFoundError: No module named 'n ...

随机推荐

  1. [转] --- Error: “A field or property with the name was not found on the selected data source” get only on server

    Error: “A field or property with the name was not found on the selected data source” get only on ser ...

  2. .NET 配置项扩展

    using System; using System.Configuration; namespace ConsoleApplication3 { /* web.config 或 app.config ...

  3. windbg命令----!idt

    !idt扩展显示指定的中断分配表(interrupt dispatch table (IDT))中的中断服务例程(interrupt service routine (ISR)) -a 没有指定IDT ...

  4. <一>Angular.js学习

    angular.module(name, [a], [b]);  // angular.module()创建.获取.注册angular中的模块 name:字符串类型,代表模块的名称: a:字符串的数组 ...

  5. U-boot中的FDT

    1. U-boot为了支持FDT,添加了新的代码:/libfdt目录fdt.h libfdt.h fdt_support.h fdt_support.c 2. http://blog.csdn.net ...

  6. linux命令(4):mkdir命令

    linux mkdir 命令 用来创建指定的名称的目录,要求创建目录的用户在当前目录中具有写权限,并且指定的目录名不能是当前目录中已有的目录. 1.命令格式: mkdir [选项] 目录... 2.命 ...

  7. 四、解决MyEclipse控制台输出中文乱码的问题

    问题描述:       在Java程序中,在MyEclipse开发环境下,通过标准输入输入中文,并把输入的中文信息从标准输出显示出来,这时中文出现乱码情况.解决方法:解决方法需要两个步骤(本文测试环境 ...

  8. Spring bean 实现初始化、销毁方法的方式及顺序

    Spring 允许 Bean 在初始化完成后以及销毁前执行特定的操作,常用方法有三种: 使用注解,在指定方法上加上@PostConstruct或@PreDestroy注解来制定该方法是在初始化之后还是 ...

  9. eclipse黑色主题

    Lennon http://www.cnblogs.com/csulennon/p/4231405.html

  10. laravel之缓存配置文件

    清除之前配置文件缓存,并生成新的配置文件缓存, get bash: php artisan config:cache 如下图,以上命令会将config文件夹下所有的文件内容缓存到bootstrap/c ...