2017-07-05 12 views
0

私はPythonとGUIが全く新しいです。私は、2つのファイルブラウザオプションとコマンドボタンを持つTkinterを使って簡単なGUIを作成しようとしています。Python 2.7 Tkinterを使用したGUI

最初の参照ボタンをクリックしてファイルを選択しようとすると、両方のテキストボックスにファイル名が表示されます。同様に、2番目のボタンをクリックすると、新しいファイル名が両方のテキストボックスに表示されます。私が間違っているところで私を修正してください。私は静かに修正するために多くを試みた。また、そのような単純なGUIのためにLOCを減らすために、コーディングを改善するための提案をしてください。

Issue Sceenshot

import os 
import Tkinter 
import tkFileDialog 
from tkFileDialog import askopenfilename 
from Tkinter import * 

file_path_1 = '' 
file_path_2 = '' 

#~~~~ FUNCTIONS~~~~ 

def open_rules_file(): 
    global file_path_1 
    filename1 = askopenfilename() 
    file_path_1 = os.path.abspath(filename1) 
    print file_path_1 
    entry1.delete(0, END) 
    entry1.insert(0, file_path_1) 

def open_src_file(): 
    global file_path_2 
    filename2 = askopenfilename() 
    file_path_2 = os.path.abspath(filename2) 
    print file_path_2 
    entry2.delete(0, END) 
    entry2.insert(0, file_path_2) 

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

root = Tk() 
root.title('MY GUI') 
root.geometry("1000x300+250+100") 

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

file_path_1 = StringVar 
file_path_2 = StringVar 

Label(f1,text="Select Rules Sheet (xls)").grid(row=1, column=0, sticky='e') 
entry1 = Entry(f1, width=70, textvariable=file_path_1) 
entry1.grid(row=1,column=1,padx=2,pady=2,sticky='we',columnspan=25) 
Button(f1, text="Browse1", command=open_rules_file).grid(row=1, column=27, sticky='ew', padx=8, pady=4) 

Label(f2,text="Select COBOL Source ").grid(row=2, column=0, sticky='e') 
entry2 = Entry(f2, width=70, textvariable=file_path_2) 
entry2.grid(row=2,column=1,padx=2,pady=2,sticky='we',columnspan=25) 
Button(f2, text="Browse2", command=open_src_file).grid(row=2, column=27, sticky='ew', padx=8, pady=4) 

Button(f3, text='Quit', command=f3.quit).grid(row=3, column=0, sticky=W, pady=4) 
root.mainloop() 
+0

私のマシンで問題を再現できません。 –

+0

私はスクリーンショットを含めるために自分の投稿をudpatedしました。親切に同じことを確認してください。 – Anijan

+0

私はまた問題を再現することができません、私のためにうまく動作します。 – cbcoutinho

答えて

0

代わりSTRINGVARクラスのオブジェクトを作成するあなたはちょうどあなたがエントリウィジェットを使用しているので、あなたはまったくtextvariable必要はありませんこれらの変数

import os 
import Tkinter 
import tkFileDialog 
from tkFileDialog import askopenfilename 
from Tkinter import * 

file_path_1 = '' 
file_path_2 = '' 

#~~~~ FUNCTIONS~~~~ 

def open_rules_file(): 
    global file_path_1 
    filename1 = askopenfilename() 
    file_path_1 = os.path.abspath(filename1) 
    print file_path_1 
    entry1.delete(0, END) 
    entry1.insert(0, file_path_1) 

def open_src_file(): 
    global file_path_2 
    filename2 = askopenfilename() 
    file_path_2 = os.path.abspath(filename2) 
    print file_path_2 
    entry2.delete(0, END) 
    entry2.insert(0, file_path_2) 

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

root = Tk() 
root.title('MY GUI') 
root.geometry("1000x300+250+100") 

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

##Instead of creating object of that class you have just assigned class to those variable 
file_path_1 = StringVar() 
file_path_2 = StringVar() 

Label(f1,text="Select Rules Sheet (xls)").grid(row=1, column=0, sticky='e') 
entry1 = Entry(f1, width=70, textvariable=file_path_1) 
entry1.grid(row=1,column=1,padx=2,pady=2,sticky='we',columnspan=25) 
Button(f1, text="Browse1", command=open_rules_file).grid(row=1, column=27, sticky='ew', padx=8, pady=4) 

Label(f2,text="Select COBOL Source ").grid(row=2, column=0, sticky='e') 
entry2 = Entry(f2, width=70, textvariable=file_path_2) 
entry2.grid(row=2,column=1,padx=2,pady=2,sticky='we',columnspan=25) 
Button(f2, text="Browse2", command=open_src_file).grid(row=2, column=27, sticky='ew', padx=8, pady=4) 

Button(f3, text='Quit', command=f3.quit).grid(row=3, column=0, sticky=W, pady=4) 
root.mainloop() 
0

にクラスを割り当てています。それらを削除し、ウィジェット独自のメソッドを使用してください。

def open_rules_file(): 
    filename1 = askopenfilename() 
    print(filename1) 
    entry1.delete(0, END) 
    entry1.insert(0, filename1) 

def open_src_file(): 
    filename2 = askopenfilename() 
    print(filename2) 
    entry2.delete(0, END) 
    entry2.insert(0, filename2) 

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

root = Tk() 
root.title('MY GUI') 
root.geometry("1000x300+250+100") 

mf = Frame(root) 
mf.pack() 

f1 = Frame(mf, width=600, height=250) 
f2 = Frame(mf, width=600, height=250) 
f3 = Frame(mf, width=600, height=250) 

f1.pack(fill=X) 
f2.pack() 
f3.pack() 

Label(f1,text="Select Rules Sheet (xls)").grid(row=1, column=0, sticky='e') 
entry1 = Entry(f1, width=70) 
entry1.grid(row=1,column=1,padx=2,pady=2,sticky='we',columnspan=25) 
Button(f1, text="Browse1", command=open_rules_file).grid(row=1, column=27, sticky='ew', padx=8, pady=4) 

Label(f2,text="Select COBOL Source ").grid(row=2, column=0, sticky='e') 
entry2 = Entry(f2, width=70) 
entry2.grid(row=2,column=1,padx=2,pady=2,sticky='we',columnspan=25) 
Button(f2, text="Browse2", command=open_src_file).grid(row=2, column=27, sticky='ew', padx=8, pady=4) 

Button(f3, text='Quit', command=f3.quit).grid(row=3, column=0, sticky=W, pady=4) 
root.mainloop() 

しかし、あなたが実際にそれを使う、という場合は、file_path_1.set("your value")を使用してStringVarの値を変更することができます。

あなたの使用状況(file_path_1 = "value")は、file_path_1を平文の文字列に変更します。

def open_rules_file(): 
    var.set(askopenfilename()) 

var = StringVar() 
entry1 = Entry(f1, width=70, textvariable = var) 
関連する問題