2017-05-19 4 views
-1

に保存すると、コードです:Tkinterではentry.get()を使用してテキストを取得し、以下の変数

import os 
    from tkinter.filedialog import askopenfilename 
    from tkinter import * 

#~~~~ FUNCTIONS~~~~ 

    def open_file_1(): 
     global file_path_1 

     filename_1 = askopenfilename() 
     file_path_1 = os.path.dirname(filename_1) + filename_1 
     entry_1.delete(0, END) 
     entry_1.insert(0, file_path_1) 
     return file_path_1 

    def open_file_2(): 
     global file_path_2 

     filename_2 = askopenfilename() 
     file_path_2 = os.path.dirname(filename_2) + filename_2 
     entry_3.delete(0, END) 
     entry_3.insert(0, file_path_2) 
     return file_path_2 

     def title_name_1(): 
      global title_1 
      title_1=str(entry_4.get()) 
      return title_1 

     def title_name_2(): 
      global title_2 
      title_2=str(entry_5.get()) 
      return title_2 

    def process_file(content): 
     print(file_path_1) 
     print(file_path_2) 
     print(title_1) 
     print(title_2) 

#~~~~~~ GUI ~~~~~~~~ 

root = Tk() 
root.title('Test Project') 
root.geometry("698x220+350+200") 

mf = Frame(root) 
mf.pack() 


f1 = Frame(mf, width=600, height=250) 
f1.pack(fill=X) 
f3 = Frame(mf, width=600, height=250) 
f3.pack(fill=X) 
f4 = Frame(mf, width=600, height=250) 
f4.pack(fill=X) 
f5 = Frame(mf, width=600, height=250) 
f5.pack(fill=X) 

f2 = Frame(mf, width=600, height=250) 
f2.pack() 



file_path_1 = StringVar 
file_path_2 = StringVar 
title_1 = StringVar 
title_2 = StringVar 


Label(f1,text="Select File 1").grid(row=0, column=0, sticky='e') 
Label(f3,text="Select File 2").grid(row=0, column=0, sticky='e') 
Label(f4,text="Title1").grid(row=0, column=0, sticky='e') 
Label(f5,text="Title2").grid(row=0, column=0, sticky='e') 

entry_1 = Entry(f1, width=50, textvariable=file_path_1) 
entry_1.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25) 
Button(f1, text="Browse", command=open_file_1).grid(row=0, column=27, sticky='ew', padx=8, pady=4) 


entry_3 = Entry(f3, width=50, textvariable=file_path_2) 
entry_3.grid(row=0,column=1,padx=2,pady=2,sticky='we',columnspan=25) 
Button(f3, text="Browse", command=open_file_2).grid(row=0, column=27, sticky='ew', padx=8, pady=4) 

entry_4 = Entry(f4, width=50,textvariable=title_1) 
entry_4.grid(row=0,column=1,padx=5,pady=2,sticky='we',columnspan=25) 

entry_5 = Entry(f5, width=50,textvariable=title_2) 
entry_5.grid(row=0,column=1,padx=5,pady=2,sticky='we',columnspan=25) 

Button(f2, text="Submit", width=32, command=lambda: process_file(content)).grid(sticky='ew', padx=10, pady=70) 

root.mainloop() 

私はこれらの4つのフィールド(file_path_1file_path_2Title1Title2)を取得したいですさらに操作するために使用できるように保存します。参照を使用してファイルを選択すると、ユーザーはTitle1とTitle2のテキストを入力します。これは新しいのであまり知られていません。

+0

これはあまりにもある:あなたがそれらにobserver callbacksを添付する場合はSTRINGVAR年代には、エントリウィジェットの時間のほとんどは、それはそれを使用する前に、.get()方法で自分のコンテンツを取得した方が良いです...

ほとんどが便利です多くのコード。以下のアドバイスを読んで、それに従ってください:[最小限で完全で検証可能な例の作成方法](http://stackoverflow.com/help/mcve) –

+0

あなたが望むものを教えてくれましたが、あなたは尋ねていません質問。あなたはどんな特定の問題を抱えていますか? –

答えて

0

StringVar()を使用すると、あなたの人生が不必要に複雑になります。

def process_file(): 
    # Get Entry box content 
    filename_1 = entry_1.get() 
    # Do something with it 
    print(filename_1) 
関連する問題