前言:在完成前后端的搭建后,通过代码将其连接起来,成为一个整体,能真正通过图形用户界面操作运行的程序!这一章就带着你一步一步完善上一章搭建好的前后端。 |
完善 GUI 界面 在上一章中制作了第一个 GUI 界面。这个界面是“平面”的,是无法互动的,点击按钮没有任何反应。按钮调用
# -*- coding:utf-8 -*-
import Tkinter as tk import tkFileDialog #<<注释1>>
class MyGUI(object): def __init__(self): self.root = tk.Tk() self.root.geometry("450x600+800+200") #大小位置 self.root.title("GIS") #设置名称 # run function self.create_widget() self.create_run_button() self.root.mainloop() # 执行循环
def create_widget(self): self.frame1 = tk.Frame(self.root, relief="raised", bd=3) self.frame1.pack(fill="x") self.entry = tk.Entry(self.frame1) self.entry.pack(side="left",expand=True, fill="x", pady=8, padx=10) self.but = tk.Button(self.frame1, text=u"输入线要素", relief="groove", width=10) self.but.config(command=self.open_dialog) #<<注释2>> self.but.pack(side="right", pady=8) def open_dialog(self): #<<注释3>> tkFileDialog.askopenfilename() def create_run_button(self): # 生成下方的“运行”按钮 self.bottom_frame=tk.Frame(self.root,relief="raised",bd=3) self.bottom_frame.pack(side="bottom",fill="x",anchor="s") self.ok_button =tk.Button( self.bottom_frame,text=u"运行",relief="groove", width=10) self.ok_button.pack(side="right", pady=8) if __name__ == '__main__': MyGUI() <<注释1>>:tkFileDialog 是属于 Tkinter 框架下的对话框创建模块。注意该模块在 Python3 中的导入方式和Python2 不一样。<<注释2>>:self.but.config(command=self.open_dialog) 和 self.but = tk.Button(self.frame1, command=self.open_dialog) 的效果是等价的。command 参数需要我们传入一个函数或者方法(甚至包括类),当点击该按钮时会触发该函数或者方法。我们传入方法 open_dialog:当鼠标点击该按钮时程序执行 open_dialog 方法来打开文件选择对话框。Note: 传入的函数、方法或者类都不能在后面添加小括号 ()。因为()表示执行前面的字符串所引用的内存地址。如果加了()符号,程序运行时就会自动运行传入的方法、函数或者类,但是我们在这里的需求是点击按钮才执行。 |

更新文本框
# -*- coding:utf-8 -*-
import Tkinter as tk import tkFileDialog
class MyGUI(object): def __init__(self): self.root = tk.Tk() self.root.geometry("450x600+800+200") #设置窗口大小、位置 self.root.title("GIS") #设置程序名称 self.var = tk.StringVar() #<<注释1>> # run function self.create_widget() self.create_run_button() self.root.mainloop() # 执行循环
def create_widget(self): self.frame1 = tk.Frame(self.root, relief="raised", bd=3) self.frame1.pack(fill="x") self.entry = tk.Entry(self.frame1) self.entry.config(textvariable=self.var) #<<注释2>> self.entry.pack(side="left",expand=True, fill="x", pady=8, padx=10) self.but = tk.Button(self.frame1, text=u"输入线要素", relief="groove", width=10) self.but.config(command=self.open_dialog) self.but.pack(side="right", pady=8) def open_dialog(self): varrr = tkFileDialog.askopenfilename() self.var.set(varrr) #<<注释3>> def create_run_button(self): # 生成下方的“运行”按钮 self.bottom_frame = tk.Frame(self.root,relief="raised",bd=3) self.bottom_frame.pack(side="bottom",fill="x",anchor="s") self.ok_button =tk.Button( self.bottom_frame,text=u"运行",relief="groove", width=10) self.ok_button.pack(side="right", pady=8) if __name__ == '__main__': MyGUI() <<注释1>>:创建一个字符串变量。用来和 Entry 绑定。<<注释2>>:将字符串变量和 Entry 的默认值绑定到一起。当字符串变量更新的时候,文本框会自动显示变量。
启动键
 现在已经知道了 tk.Button 类中的 command 参数可以指定一个函数或者方法(包括类),点击该按钮时就会触发。所以只需要将后端的主程序当做 tk.Button 类的参数传入就行。修改后端代码 在传入之前修改了第三章的后端代码,使其更简洁和简单。在原来的基础上改动了工作空间的配置:因为后端的数据处理等工作很简单所以就没有使用 GDB 数据库作为工作空间,而是使用当前代码所在的文件夹作为工作空间。主要功能没有发生变化,依然是:将一个线矢量文件转换成面矢量文件,然后给这个面矢量文件添加四个不同的字段。# -*- coding:utf-8 -*- # --------------------------------------------------------------------------- # Created on: 2021/1/17 14:53
import arcpy import os
def main(line_shp): #<<注释1>> """ 程序运行主函数 :param line_shp: 线矢量文件 :return: None """ arcpy.env.workspace = os.getcwd() #<<注释2>> arcpy.env.overwriteOutput = True # 线要素转面,生成的面矢量名称为 polygon arcpy.FeatureToPolygon_management(line_shp, "polygon.shp") print "Create polygon" # 给 polygon 图层添加字段 for name in ["CJQYMC", "CJQYDM", "XJQYMC", "XJQYDM"]: arcpy.AddField_management("polygon.shp", name, "TEXT", field_length=100) print "Add field complete" print "Done" <<注释1>>:将原来的“散装”代码封装成函数。没有改变功能。<<注释2>>:用于设置工作空间。将源代码文件所在的文件夹作为默认工作空间。连接 在修改完前后端代码后,现在正式将两部分连接起来。# -*- coding:utf-8 -*-
import Tkinter as tk import tkFileDialog import giscode #<<注释1>>
class MyGUI(object): def __init__(self): self.root = tk.Tk() self.root.geometry("450x600+800+200") #设置窗口大小、位置 self.root.title("GIS") #设置程序名称 self.var = tk.StringVar() # run function self.create_widget() self.create_run_button() self.root.mainloop() # 执行循环
def create_widget(self): self.frame1 = tk.Frame(self.root, relief="raised", bd=3) self.frame1.pack(fill="x") self.entry = tk.Entry(self.frame1) self.entry.config(textvariable=self.var) self.entry.pack(side="left",expand=True, fill="x", pady=8, padx=10) self.but = tk.Button(self.frame1, text=u"输入线要素", relief="groove", width=10) self.but.config(command=self.open_dialog) self.but.pack(side="right", pady=8) def open_dialog(self): varrr = tkFileDialog.askopenfilename() self.var.set(varrr) def create_run_button(self): # 生成下方的“运行”按钮 self.bottom_frame = tk.Frame(self.root,relief="raised",bd=3) self.bottom_frame.pack(side="bottom",fill="x",anchor="s") self.ok_button =tk.Button( self.bottom_frame,text=u"运行",relief="groove", width=10) self.ok_button.pack(side="right", pady=8) self.ok_button.config(command=self.run) #<<注释2>> def run(self):#<<注释3>> giscode.main(self.var.get()) if __name__ == '__main__': MyGUI() <<注释1>>:导入后端代码。前端和后端代码是分离的。创建 GUI 的前端代码文件是 makeGUI.py;后端代码文件是 giscode.py。 <<注释2>>&<<注释3>>:新创建一个方法,该方法用于启动后端功能代码。
运行视频
结束语 链接:https://pan.baidu.com/s/1UIkgPGe7AjHkqm7vsz9p7w
|