【发布时间】:2019-04-21 17:39:50
【问题描述】:
我有一个 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()
但我不明白如何链接这两个代码。
当我单击用户界面中的“生成地图”按钮时,我希望用户界面框中给出的输入和输出的位置/路径移动到第一个代码中各自的位置,然后自动运行相同的代码.
请帮助我实现我的要求。
【问题讨论】:
-
把你的代码放在函数中,即。
def my_code()并点击按钮Button( command=my_code) -
@furas 如果我这样做,它会做什么。
-
当你按下按钮时它会运行你的代码。
-
@没关系。传递输入和输出位置呢?
标签: python user-interface tkinter