栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python自带的Tkinter写GUI小程序

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python自带的Tkinter写GUI小程序

学了一点点python基础语法,就斗胆用它加上一些文件读写的module,写了几个文件处理的程序;
但是需求又说:写个简单的GUI吧,用起来方便。

目标:GUI实现

  • 点击选择文件按钮弹出windows的文件管理器,选取所需要的文件/文件夹

  • 将上述的路径显示出来

  • 点击运行按钮,运行文件处理程序

效果图

工具:选择了python自带的tkinter

参考资料:
http://www.tutorialspoint.com/python/python_gui_programming.htm
简直是最全面的资料!

简单介绍一下最关键的部分:
首先要有一个基本的框架,我们在这个框架里面添加其他的框架:
root = Tk() #base window
这个root就是主框架名

框架布局

然后我们计划一下,程序有哪几个部分,因为要设计布局,而tkinter非常简单,只能定义上下左右4个方向;我们定义两个主要的部分,左边和右边:

buttonfrm = frame(root)         #'button frame' is in Root windowbuttonfrm.pack()

textframe = frame(root)       #text frame in Root windowtextframe.pack(side=LEFT)

textframe 和 buttonframe 是两个副框架名

然后我们在里面去定义组件。

语法:#

因为上面那个连接是最全面的,我就不多说了,尽量参考原文档。

按钮:

B = Tkinter.Button(top, text ="Hello", command = function)

字符框:

可实时更新哦(用下面的set函数)

var = StringVar()
label = Label( root, textvariable=var, relief=RAISED )var.set("Hey!? How are you doing?")
label.pack()
示例:
import Tkinterimport tkMessageBox
top = Tkinter.Tk()def helloCallBack():
  tkMessageBox.showinfo( "Hello Python", "Hello World")
  B = Tkinter.Button(top, text ="Hello", command = helloCallBack)
  B.pack()
top.mainloop()

示例效果

python 代码:

import tkinterfrom tkinter import *global s

s='sssss'def func1():
    s='fucked!'
    outputtext.set(s)

root = Tk()                     #base windowbuttonfrm = frame(root)         #'button frame' is in Root windowbuttonfrm.pack()

textframe = frame(root)       #text frame in Root windowtextframe.pack(side=LEFT)

btnfrm=frame(root)
btnfrm.pack(side=BOTTOM)

inputbutton = Button(buttonfrm, text="Input",command=func1)       #command = helloCallBackinputbutton.pack()

outputbutton = Button(buttonfrm, text="Output")
outputbutton.pack()




inputtext=StringVar()
inputmsg=Label(textframe,textvariable=inputtext, relief=RAISED)
inputtext.set(s)
inputmsg.pack()

s='changed!'outputtext=StringVar()
outputmsg=Label(textframe,textvariable=outputtext, relief=RAISED)
outputtext.set(s)
outputmsg.pack()#run & exit buttonrunbutton = Button(btnfrm, text="RUN!",fg='red')
runbutton.pack()

exitbutton = Button(btnfrm, text="EXIT!",command=root.quit)
exitbutton.pack()

root.mainloop()


作者:AwesomeAshe
链接:https://www.jianshu.com/p/67696cc14734


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/221848.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号