python - 如何使用用户界面为 python 代码提供“输入”和“输出”的位置并从 UI 本身运行代码?
问题描述
我有一个 python 代码:
import gdal
import numpy
from skimage.filters import threshold_otsu
ds = gdal.Open('A:\\algo\\f2.tif')
在这段代码中,第 4 行给出了“输入文件”的位置/路径,第 10 行给出了“输出文件”的位置。
我想创建一个用户界面以在用户界面本身中提供此位置并从用户界面本身运行代码。
我尝试使用“tkinter”模块制作用户界面:
from tkinter import *
from tkinter import filedialog
def input():
file1 = filedialog.askopenfile()
label = Label(text=file1).pack()
def input2():
file2 = filedialog.asksaveasfile(mode="w", defaultextension=".tif")
label = Label(text=file2).pack()
w = Tk()
w.geometry("500x500")
w.title("FLOOD_MAPPER")
h = Label(text = "S1A FLOOD MAPPER", bg = "yellow", fg = "black", height = "3", width = "500")
h.pack()
i1 = Label(text = "Input*")
i1.place(x=10, y=70)
i1b = Button(w, text = "Select File", command =input)
i1b.place(x=250, y=70)
i2 = Label(text = "Intermediate Product*")
i2.place(x=10, y=140)
i2b = Button(w, text = "Save as", command =input2)
i2b.place(x=250, y=140)
button = Button(w, text="Generate Map", bg = "red", fg = "black", height = "2", width="30")
button.place(x=150, y=400)
w.mainloop()
但我不明白如何链接这两个代码。
当我单击用户界面中的“生成地图”按钮时,我希望用户界面框中给出的输入和输出的位置/路径移动到第一个代码中各自的位置,然后自动运行相同的代码。
请帮助我实现我的要求。
解决方案
它可以看起来像这样。我删除了只保留 tkinter 中的重要元素。
我输入代码your_code
,它可以获取文件名作为参数。所以这段代码看起来和以前一样。
我创建了使用分配给全局变量的文件名gen_map
运行的函数,`output_filename.your_code
input_filename
我gen_map
对按钮进行了评估,Button( command=gen_map)
因此当您按下按钮时它会运行它。
其他按钮打开对话框以获取文件名并分配给全局变量input_filename
,output_filename
.
from tkinter import *
from tkinter import filedialog
import gdal
import numpy
from skimage.filters import threshold_otsu
def your_code(input_file, output_file):
#ds = gdal.Open('A:\\algo\\f2.tif')
ds = gdal.Open(input_file)
band = ds.GetRasterBand(1)
arr = band.ReadAsArray()
thresh = threshold_otsu(arr,16)
binary = arr > thresh
driver = gdal.GetDriverByName("GTiff")
#outdata = driver.Create("A:\\algo\\test11.tif", 14823, 9985, 1, gdal.GDT_UInt16)
outdata = driver.Create(output_file, 14823, 9985, 1, gdal.GDT_UInt16)
outdata.SetGeoTransform(ds.GetGeoTransform())
outdata.SetProjection(ds.GetProjection())
outdata.GetRasterBand(1).WriteArray(binary)
outdata.GetRasterBand(1).SetNoDataValue(10000)
outdata.FlushCache() ##saves to disk!!
#outdata = None
#band = None
#ds = None
def get_input_filename():
global input_filename
# `askopenfilename` instead of `askopenfile` to get filename instead of object file
input_filename = filedialog.askopenfilename()
input_label['text'] = input_filename
def get_output_filename():
global output_filename
# `asksaveasfilename` instead of `asksaveasfile` to get filename instead of object file
output_filename = filedialog.asksaveasfilename(defaultextension=".tif")
output_label['text'] = output_filename
def gen_map():
#global input_filename
#global output_filename
print('input:', input_filename)
print('output:', output_filename)
your_code(input_filename, output_filename)
#---------------------------------------------
# global variables with default values at start
input_filename = 'A:\\algo\\f2.tif'
output_filename = "A:\\algo\\test11.tif"
root = Tk()
#input_label = Label(root, text=input_filename)
input_label = Label(root, text="Input*")
input_label.pack()
input_button = Button(root, text="Select File", command=get_input_filename)
input_button.pack()
#output_label = Label(root, text=output_filename)
output_label = Label(root, text="Intermediate Product*")
output_label.pack()
output_button = Button(root, text="Save as", command=get_output_filename)
output_button.pack()
gen_map_button = Button(root, text="Generate Map", command=gen_map)
gen_map_button.pack()
root.mainloop()
推荐阅读
- angular - 无法使用发布请求发送表单数据
- php - nginx 代理 cookie 域
- java - Java - 无法使用 File .delete() 删除文件
- c++ - Bazel 构建命令行将 copt 参数传递给特定的 cc_library/cc_binary?
- java - 千分尺(micrometerClock)中的错误处理条件
- javascript - 在 Chrome 的浏览器控制台中调用 clearInterval
- swift - 使用结果调用函数
- c# - 将 GUI.Label 绘制到鼠标位置
- elasticsearch - 在 Kibana /Elastic Search 中聚合嵌套字段
- angular - angular,fb 仪表板(留言板),如何在组件模板中设置 url