今天看到了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. I2C总线和S5PV210的I2C总线控制器

    一.什么是I2C通信协议? 1.物理接口:SCL + SDA (1)SCL(serial clock):时钟线,传输CLK信号,一般是I2C主设备向从设备提供时钟的通道. (2)SDA(serial ...

  2. 在GMIC听“移动互联网+医疗”的感受 2015-04-29

    “互联网+”这个词挺火的,基本格式是“互联网+传统行业”,比如医疗.教育.交通等等.就更别说电子商务.金融这些领域了,相比已经和互联网分不 开了.在我看来,互联网+的背后,是信息化.智能化.信息沟通的 ...

  3. Facade(外观)-对象结构型模式

    1.意图 为子系统中的一组接口提供一个一致的接口,Facade模式定义了一个高层接口,这个接口使得这一子系统更加容易使用. 2.动机 将一个系统划分成若干子系统有利于降低系统的复杂性.一个常见的设计目 ...

  4. Unity3D中制作Loading场景进度条

    背景 通常游戏的主场景包含的资源较多,这会导致加载场景的时间较长.为了避免这个问题,可以首先加载Loading场景,然后再通过Loading场景来加载主场景.由于Loading场景包含的资源较少,所以 ...

  5. (引用)Python 元素、元组、列表、字典的区别

    转载:http://www.th7.cn/Program/Python/201407/231060.shtml 元素: 元组:tuple=('nice','to','meet','you') 列表:l ...

  6. jquery取框架当前的url

    前提,当前frame无名或动态生成的: var url = eval($("#ifr1").attr("name")+".location.href; ...

  7. 使用id名称和name直接获取元素

    我们知道一些第三方的js库对如何快速选取html中的元素做了一些简化,貌似十分高深莫测,其实也不然.而且js本身自带了对于特殊元素的简便选取的方法,下面就为大家简单介绍下. 在html中,一般最直接的 ...

  8. Oracle重启 error: ora-01034:oracle not available ora-27101:shared memory realm does not exist

    error: ora-01034:oracle not available ora-27101:shared memory realm does not exist 苦咖啡 他的博客中一篇文章完美的解 ...

  9. linux 开机自添加路由

    进入 vi /etc/sysconfig/network/routes Destination     Gateway            Genmask         Use Iface172. ...

  10. 又一次的Microsoft Visual C++ 10.0 is required (Unable to find vcvarsall.bat)

    ~~~~~~~~~~~My problem is here~~~~~~~~~~~~~~~~~~~~~~ Error: Microsoft visual C++ 10.0 is required (un ...