简介
接上一次的tkinter编写界面相关内容,丰富点常用控件的方法,学会了这些控件布局和相关方
法属性,能够满足日常小工具的制作需求了。
搭建的界面框架如下图所示,功能可以自己添加。
界面代码
# -*- coding: utf-8 -*-
import os
from tkinter import *
from tkinter import messagebox
from tkinter.filedialog import askopenfilename
from os.path import isfile
fileName = ''
def func1(strfile):
print('fun1' + strfile)
def func2(strfile):
print('fun2' + strfile)
def fun3(strfile):
print('fun3' + strfile)
def fileopen():
v.set("")
txtinfo.delete('0.0', END)
global fileName
fileName = askopenfilename(title='choose data file', filetypes=[('text file', ('.dat', '.txt', '.log'))])
v.set(fileName)
def start_main(mode):
txtinfo.delete('0.0', END)
if not fileName or not os.path.exists(fileName):
messagebox.showwarning('提示', '请选择正确的文件路径名!')
return
if mode == 0:
txtinfo.insert('0.0', '功能1运行\n')
if isfile(fileName):
try:
func1(fileName) # 功能1主函数,输入文件名
except IOError:
txtinfo.insert(INSERT, '失败')
elif mode == 1:
txtinfo.insert('0.0', '功能2运行\n')
if isfile(fileName):
try:
func2(fileName) # 功能2主函数,输入文件名
except IOError:
txtinfo.insert(INSERT, '失败')
else:
txtinfo.delete('0.0', END)
txtinfo.insert('0.0', '功能3运行\n')
if isfile(fileName):
try:
fun3(fileName) # 功能3主函数,输入文件名
except IOError:
txtinfo.insert(INSERT, '失败')
def checkbox_1_repose():
# 获取复选框1的状态,勾选与否
print(check_1_var.get())
def checkbox_2_repose():
# 获取复选框2的状态,勾选与否
print(check_2_var.get())
if __name__ == '__main__':
width, height = 580, 380
frameT = Tk()
screenwidth = frameT.winfo_screenwidth()
screenheight = frameT.winfo_screenheight()
position = '%dx%d+%d+%d' % (width, height, (screenwidth - width) / 2, (screenheight - height) / 2)
frameT.geometry(position)
frameT.title('GUI演示')
frame1 = Frame(frameT)
frame1.pack(fill=X)
v = StringVar()
ent = Entry(frame1, width=60, bd=2, textvariable=v)
ent.pack(side=LEFT)
v.set('文件路径....')
but = Button(frame1, text='打开文件', font=("宋体", 10), command=fileopen)
but.pack(fill=X, padx=4, pady=10)
frame2 = Frame(frameT)
frame2.pack(fill=X)
keyVariable = StringVar()
# 默认解析的语句
keyVariable.set('编辑框使用')
keyWord = Entry(frame2, bd=2, textvariable=keyVariable)
keyWord.pack(fill=X, padx=6, pady=2)
Label(frame2, text='文字说明下面的编辑框作用').pack(anchor='w')
Label(frame2, text='类型').pack(side=LEFT)
pos_unit = StringVar()
pos_unit.set("篮球")
OptionMenu(frame2, pos_unit, "篮球", "其他").pack(side=LEFT, padx=4, pady=8)
# 创建lat/lon/hgt等索引
Label(frame2, text='标签1').pack(side=LEFT, padx=4, pady=8)
lat_index = IntVar()
Entry(frame2, width=4, bd=2, textvariable=lat_index, justify='center').pack(side=LEFT, padx=4, pady=8)
lat_index.set('2')
Label(frame2, text='标签2').pack(side=LEFT, padx=4, pady=8)
lon_index = IntVar()
Entry(frame2, width=4, bd=2, textvariable=lon_index, justify='center').pack(side=LEFT, padx=4, pady=8)
lon_index.set('3')
Label(frame2, text='标签3').pack(side=LEFT, padx=4, pady=8)
hgt_index = IntVar()
Entry(frame2, width=4, bd=2, textvariable=hgt_index, justify='center').pack(side=LEFT, padx=4, pady=8)
hgt_index.set('4')
# 降采样区域
frame_sample = Frame(frameT)
frame_sample.pack(fill=X)
Label(frame_sample, text='周期').pack(side=LEFT, pady=2)
sample_var = IntVar()
sample_var.set('100') # 设置初始值
Entry(frame_sample, width=8, textvariable=sample_var, justify='center').pack(side=LEFT, padx=2, pady=10)
check_2_var = StringVar()
check_2_var.set('1') # 1设置勾选
Checkbutton(frame_sample, text="复选框2", variable=check_2_var, command=checkbox_2_repose).pack(side=RIGHT, padx=2)
check_1_var = StringVar()
check_1_var.set('0') # 0不勾选
Checkbutton(frame_sample, text="复选框1", variable=check_1_var, command=checkbox_1_repose).pack(side=RIGHT, padx=2)
Label(frame_sample, text='复选框').pack(side=RIGHT)
# 信息显示区域
frame_info = Frame(frameT)
frame_info.pack(fill=X)
txtinfo = Text(frame_info, bd=2, height=10)
txtinfo.pack(fill=X, padx=2)
txtinfo.insert(INSERT, '该区域显示结果\n ')
# 画图区域
frame3 = Frame(frameT)
frame3.pack(fill=X, pady=10)
bdbut = Button(frame3, width=8, bd=2, height=1, text='功能1', font=("宋体", 14),
command=lambda mode=0: start_main(mode))
bdbut.pack(side=LEFT, padx=5, pady=2)
gdbut = Button(frame3, width=8, bd=2, height=1, text='功能2', font=("宋体", 14),
command=lambda mode=1: start_main(mode))
gdbut.pack(side=LEFT, padx=5, pady=2)
ggbut = Button(frame3, width=8, bd=2, height=1, text='功能3', font=("宋体", 14),
command=lambda mode=2: start_main(mode))
ggbut.pack(side=LEFT, padx=5, pady=2)
closebut1 = Button(frame3, width=10, height=1, bd=2, text='关闭',
font=("宋体", 14), command=frameT.quit)
closebut1.pack()
frameT.mainloop()
打包exe文件
打包的方法见下面的博客:python gui软件打包exe可执行程序