简体   繁体   English

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

[英]How to give the location of “Input” and “Output” for a python code using User Interface and run the code from UI itself?

I have a python code : 我有一个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

In this code, line 4 gives the location/path of "input file" and line 10 gives the location of "output file". 在此代码中,第4行给出“输入文件”的位置/路径,第10行给出“输出文件”的位置。

I want to create a user interface to give this location in the user interface itself and run the code from user interface itself. 我想创建一个用户界面以在用户界面本身中提供此位置,并从用户界面本身运行代码。

I have tried making a user interface using "tkinter" module : 我尝试使用“ 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()

But I didn't understand how to link these two codes. 但是我不明白如何链接这两个代码。

The moment I click on button "generate map" in the user interface I want the location/path of Input and output given in the user interface box to move to their respective places in the 1st code and then run the same code aumoatically. 当我在用户界面中单击“生成地图”按钮时,我希望在用户界面框中输入和输出的位置/路径移动到第一代码中它们各自的位置,然后自动运行同一代码。

Kindly, help me to achieve my requirement. 请帮助我达到要求。

It can look like this. 它看起来像这样。 I removed keep only important elements in tkinter. 我删除了在tkinter中仅保留重要元素。

I put code in your_code and it can get filenames as paramaters. 我将代码放在your_code ,它可以获取文件名作为参数。 So this code looks similar as before. 因此,此代码与以前相似。

I create function gen_map which get run your_code with filenames which are assigned to global variables input_filename , `output_filename. 我创建了gen_map函数,该函数使用指定给全局变量input_filename和`output_filename的文件名运行your_code

I assing gen_map to button Button( command=gen_map) so it will run it when you press button. 我将gen_map到按钮Button( command=gen_map)这样当您按下按钮时它将运行它。

Other buttons open dialog to get file names and assign to global variables input_filename , output_filename . 其他按钮打开对话框以获取文件名并将其分配给全局变量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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在python代码中给出输入文件位置和输出位置 - How to give input file location and output location inside python code Python tkinter使用变量和来自GUI的用户输入来运行代码 - Python tkinter using variables and user input from GUI to run code Python - 读取终端 output 并从脚本本身提供输入 - Python - Read terminal output and give input from the script itself 如何以交互方式将代码的输出输入到另一个代码中,并从Pycharm交互式python控制台运行它们? - How to interactively input the output of a code into another code and run them from Pycharm interactive python console? 无法使用python中的用户界面将输入和输出的位置传递给extenal命令 - Unable to pass location of input and output to an extenal command using User-Interface in python 在 python 中运行 java 代码 - 带输入/输出 - Run java code in python - with input/output 有没有办法使用stdout或重定向的输出运行python代码? - Is there a way to run python code using the output from stdout or a redirection? 在python中,如果用户没有在固定时间内输入,如何跳转到下一个代码? - In python how to jump to next code if user does not give input in a fixed time? Python从用户输入位置运行python文件 - Python Run a python file from a user input location 从Python运行Perl代码(带有输出到文件) - Run Perl code (with output to file) from Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM