2016-09-21 13 views
0

私はすべての情報を入力する必要があるフォームがあります。すべての情報を入力するだけで情報が保存されます。しかし、奇妙な小さなバグが起きました。ラジオボタンが選択されている限り、チェックボックスを選択せず​​に送信してから、その後にyesに切り替えると、データが保存されます。私はフォームのプロパティを変更したくないのですが、私の唯一の望みはif文の中でチェックするときに意味エラーを修正することです。プログラム中のif文チェック中のセマンティックエラー

#Import tkinter to make gui 
from tkinter import * 
from tkinter import ttk 
from tkinter import messagebox 

#Sets title and creates gui 
root = Tk() 
root.title("Entry Form") 

def changed1(*args): 
    if yes.get()=="1": 
     no.set('0') 
def changed2(*args): 
    if no.get()=="1": 
     yes.set('0') 

#After sumbitting with no selected and then switching to yes afterwards as long as a radiobutton is seletected, data is saved? 
def submit(*args): #Realign? 
    file = open("data.csv", "a") 
    if first.get() != "" and last.get() != "" and option.get() == 'Business' or option.get() == 'Residence' or option.get() == 'Other' and state.get() != "" and yes.get() == '1' or no.get() == '1': 
     if yes.get()=='1': 
      file.write(last.get().title() + "," + first.get().title() + "," + option.get() + "," + state.get() + '\n') 
      printer.set("Data Saved!") 
     else: 
      messagebox.showinfo("Unauthorized", "You must accept terms to continue.") 
    else: 
     messagebox.showinfo("Incomplete Information", "Please fill out all parts of the form") 

#Configures column and row settings and sets padding 
mainframe = ttk.Frame(root, padding="3 3 12 12") 
mainframe.grid(column=0, row=0, sticky=(N, W, E, S)) 
mainframe.columnconfigure(0, weight=1) 
mainframe.rowconfigure(0, weight=1) 

first = StringVar() 
last = StringVar() 
option = StringVar() 
statevar = StringVar() 
printer = StringVar() 
yes = StringVar() 
no = StringVar() 

#Widgets to put in name 
firstvar = ttk.Entry(mainframe, width=15, textvariable=first) 
firstvar.grid(column=2, row=1, sticky=(N, W)) 

lastvar = ttk.Entry(mainframe, width=15, textvariable=last) 
lastvar.grid(column=2, row=2, sticky=(N, W)) 

ttk.Label(mainframe, text="First Name").grid(column=1, row=1, sticky=(W)) 
ttk.Label(mainframe, text="Last Name").grid(column=1, row=2, sticky=(W)) 

business = ttk.Radiobutton(mainframe, text='Business', variable=option, value='Business') 
residence = ttk.Radiobutton(mainframe, text='Residence', variable=option, value='Residence') 
other = ttk.Radiobutton(mainframe, text='Other', variable=option, value='Other') 

business.grid(column=1, row=3, sticky=(W, E)) 
residence.grid(column=2, row=3, sticky=(W, E)) 
other.grid(column=3, row=3, sticky=(W, E)) 

state = ttk.Combobox(mainframe, textvariable=statevar, state='readonly') 
state.grid(column=2, row=4, sticky=(W)) 

ttk.Label(mainframe, text="State").grid(column=1, row=4, sticky=W) 
state['values'] = ('Alabama', 'Alaska', 'Arizona', 'Arkansas', 'California', 'Colorado', 'Connecticut', 'Delaware', 'Florida', 'Georgia', 'Hawaii', 'Idaho', 'Illinois', 'Indiana', 'Iowa', 'Kansas', 'Kentucky', 'Louisiana', 'Maine', 'Maryland', 'Massachussetts', 'Michigan', 'Minnesota', 'Mississippi', 'Missouri', 'Montana', 'Nebraska', 'Nevada', 'New Hampshire', 'New Jersey', 'New Mexico', 'New York', 'North Carolina', 'North Dakota', 'Ohio', 'Oklahoma', 'Oregon', 'Pennsylvannia', 'Rhode Island', 'South Carolina', 'South Dakota', 'Tennessee', 'Texas', 'Utah', 'Vermont', 'Virginia', 'Washington', 'West Virginia', 'Wisconsin', 'Wyoming') 

#Creates no checkbutton 
yesvar = ttk.Checkbutton(mainframe, text='Yes', command=changed1, variable=yes) 
yesvar.grid(column=2, row=5, sticky=(W, E)) 

#Creates yes check button 
novar = ttk.Checkbutton(mainframe, text='No', command=changed2, variable=no) 
novar.grid(column=3, row=5, sticky=(W, E)) 

ttk.Label(mainframe, text="Accept Policy").grid(column=1, row=5, sticky=(W)) 

#Adds a calculate button 
ttk.Button(mainframe, text="Submit", command=submit).grid(column=3, row=9, sticky=W) 

ttk.Label(mainframe, textvariable=printer).grid(column=2, row=9, sticky=(E)) 

root.bind('<Return>', submit) 

#Keeps the gui running 
root.mainloop() 
+0

エラーログも追加してください。 – harshil9968

+0

と 'と'と 'または'との複雑な式では、常にサブ式をグループ化するために括弧を使用する必要があります。評価の順序が期待どおりでない可能性があります。複数の行に大きな 'if 'を広げることは、読みやすさにも大きく役立ちます。 –

答えて

0

optionを複数回テストする必要はありません。
if first.get() != ""の代わりにif first.get():を使用してください。
2番目のif文でyesをテストしていますので、最初のメッセージから削除してください(別のメッセージボックスを表示する必要があります)。

# Check required fields 
if first.get() and last.get() and option.get() and state.get(): 
    # Check Constraints before saving 
    if yes.get() == '1': 
関連する問題