如何使用用户界面为python代码指定“输入”和“输出”的位置,以及如何从UI本身运行代码?

时间:2019-04-21 17:39:51

标签: python user-interface tkinter

我有一个python代码:

import gdal
import numpy
from skimage.filters import threshold_otsu
ds = gdal.Open('A:\\algo\\f2.tif')
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.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

在此代码中,第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()

但是我不明白如何链接这两个代码。

当我在用户界面中单击“生成地图”按钮时,我希望在用户界面框中指定的输入和输出的位置/路径移动到第一代码中各自的位置,然后自动运行相同的代码。

请帮助我达到要求。

1 个答案:

答案 0 :(得分:1)

它看起来可能像这样。我删除了仅将重要元素保留在tkinter中。

我将代码放在your_code中,它可以获取文件名作为参数。所以这段代码看起来和以前一样。

我创建函数gen_map,该函数以指定给全局变量your_code的`output_filename的文件名运行input_filename

我将gen_map设置为按钮Button( command=gen_map),以便在您按下按钮时它将运行它。

其他按钮打开对话框以获取文件名并分配给全局变量input_filenameoutput_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()