所以我有一个表单要求用户输入所有信息。只有输入所有信息才能保存信息。然而,一个奇怪的小错误已经出现。提交后没有选中复选框,然后切换到yes(是的,这是预期的)只要选择了一个单选按钮,就会保存数据。我不希望更改表单的属性,我唯一希望在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 :(得分:0)
您不需要多次测试option
使用if first.get():
代替if first.get() != ""
您正在第二个yes
语句中测试if
,因此请将其从第一个语句中删除(需要显示不同的消息框)。
# Check required fields
if first.get() and last.get() and option.get() and state.get():
# Check Constraints before saving
if yes.get() == '1':