2016-10-31 5 views
0

tkinterを使用して、スクリプト内で何回も操作される2つの別々のファイルを選択できるウィンドウを開きたいと思います。サブプロセスコール内で使用できるように、tkinterウィンドウ内のボタンを使って選択されるファイルを変数として設定する方法を見つけるのが難しいです。私はinvoke()が見つかりましたが、何の影響もないようです。私が何をするかについてのどんなアイデアも非常に高く評価されます。Tkinterとsuprocess.callの使用

import os 
import sys 
import gdal 
from gdalconst import * 
import numpy as np 
import math 
import subprocess 
from subprocess import call 
import math 
import datetime 
import shutil 
import tkinter 
from tkinter import * 
from tkinter import filedialog 


newpath = os.path.expanduser('~\\Desktop\\Components\\Float32') 
if not os.path.exists(newpath): 
    os.makedirs(newpath) 
newpath_2 = os.path.expanduser('~\\Desktop\\Components\\Zeros') 
if not os.path.exists(newpath_2): 
    os.makedirs(newpath_2) 
newpath_3 = os.path.expanduser('~\\Desktop\\Components\\db_Files') 
if not os.path.exists(newpath_3): 
    os.makedirs(newpath_3) 


if __name__== '__main__': 

    # Set all of the necessary constants so that the script can create and save the pertinent files 
    # on the users desktop 

    #tk1 = Tk() 
    #tk2 = Tk() 
    #callTK = 'src_dataset =' + tk1 
    #callTK_2 = 'srcVH =' + tk2 
    gdalTranslate = 'C:\Program Files (x86)\GDAL\gdal_translate.exe' 
    tk1.fileName = filedialog.askopenfilename(text="Open HV File") 
    tk2.fileName = filedialog.askopenfilename(text="Open VH File") 
    dst_dataset = os.path.expanduser('~\\Desktop\\Components\\Float32\\newHV32.img') 
    dstVH = os.path.expanduser('~\\Desktop\\Components\\Float32\\newVH32.img') 
    sttime = datetime.datetime.now().strftime('(Time_Run = %Y-%d-%m_%H:%M:%S)') 
    wheel_install_1 = os.path.expanduser('~\\Desktop\\Components\\Sigma_Test\\wheel_install.py') 
    wheel_install_2 = os.path.expanduser('~\\Desktop\\Components\\Sigma_Test\\wheel_install2.py') 
    ridofz = os.path.expanduser('~\\Desktop\\Components\\Sigma_Test\\ridofZsv2.py') 
    to_dB = os.path.expanduser('~\\Desktop\\Components\\Sigma_Test\\to_dBv2.py') 
    db_HV = os.path.expanduser('~\\Desktop\\Components\\dB_Files\\newHVdB.img') 
    db_VH = os.path.expanduser('~\\Desktop\\Components\\dB_Files\\newVHdB.img') 
    cmd = "-ot float32 -of HFA" # hopefully this works 


    # Install necessary packages, which are GDAL and Numpy 

    # try: 
     #os.system(wheel_install_1) 
     #print ("GDAL intalled") 
     #os.system(wheel_install_2) 
     #print ("Numpy installed") 

    #except: 
     #print ("The packages are't installing properly") 
     #sys.exit() 

    # Create three new folders which will house the files that will be created 
    # along each sequential step of the script 

    #newpath = os.path.expanduser('~\\Desktop\\Components\\Float32') 
    #if not os.path.exists(newpath): 
     #os.makedirs(newpath) 
    #newpath_2 = os.path.expanduser('~\\Desktop\\Components\\Zeros') 
    #if not os.path.exists(newpath_2): 
     #os.makedirs(newpath_2) 
    #newpath_3 = os.path.expanduser('~\\Desktop\\Components\\db_Files') 
    #if not os.path.exists(newpath_3): 
     #os.makedirs(newpath_3) 

    root = Tk() 
    #root.fileName = filedialog.askopenfilename() 
    root.title("Utilis Sigma Test") 
    root.iconbitmap(r"C:\Users\jack.UTILIS\Desktop\images\sigma.ico") 
    root.configure(background="#179EBB") 
    topFrame = Frame(root) 
    topFrame.pack() 

    photo = PhotoImage(file="C:\\Users\\jack.UTILIS\\Desktop\\images\\Utilis_Branding2015_FINAL_Small.gif") 
    label = Label(root, image=photo) 
    label.pack(side=RIGHT) 

    bottomFrame = Frame(root) 
    bottomFrame.pack(side=BOTTOM) 


    button1 = Button(root, text="Open HV File", fg="black", command=filedialog.askopenfilename) 
    button2 = Button(root, text="Open VH FIle", fg="black", command=filedialog.askopenfilename) 


    button1.pack(side=LEFT) 
    button2.pack(side=RIGHT) 



    hvfullCmd = ' '.join([gdalTranslate, cmd, tk1.fileName,dst_dataset]) 
    subprocess.call(hvfullCmd) 
    vhfullCmd = ' '.join([gdalTranslate,cmd, tk2.fileName,dstVH]) 
    subprocess.call(vhfullCmd) 

    root.mainloop() 
+0

を使用して、ボタンにこのファイルを割り当てることができます。 – furas

答えて

0

あなたがaskopenfilenameからファイル名を取得し、このファイルを使用して何かをする独自の関数を作成する必要があります。そして、あなたは、ファイル名を取得するために `filedialog.askopenfilename`を使用して、この名前の何かをするボタン(`コマンド= `)独自の機能に割り当てる必要がありcommand=

import tkinter as tk 
from tkinter import filedialog 

# --- functions --- 

def on_click(): 
    filename = filedialog.askopenfilename() 
    if filename: 
     print("Filename:", filename) 
    else: 
     print("Filename: not selected") 

# --- main --- 

root = tk.Tk() 

btn = tk.Button(root, text='Click Me', command=on_click) 
btn.pack() 

root.mainloop() 
+0

ありがとう!これはうまくいった。 –

関連する問題