Jan-31-2019, 06:33 PM
(Jan-30-2019, 11:15 PM)Larz60+ Wrote:Wow thanks for the codes but how can I click on the 'Cancel' button and get to clear each and every information in the Entry Field.Quote:I posted I'll play with this a bit.** Note ** Following code uses f-string which requires python 3.6 or newer Here's how I would do it, tested and works well:from tkinter import * import Pmw class MyForm: def __init__(self): self.root = Tk() self.root.title('EntryField') self.form = {} self.fields = ['First Name:','Surname','Sex:','Age:','Phone Number:','Email Address:'] self.build_form() self.root.mainloop() def build_form(self): for field in self.fields: curfield = self.form[f'{field}'] = {} curfield['var'] = StringVar() curfield['widget'] = Pmw.EntryField( self.root, labelpos= 'w', label_text=field, label_width=15, entry_width=24, entry_textvariable = curfield['var'] ) curfield['widget'].pack(side=TOP,padx=5,pady=5) button_a = Button( self.root, text='Register', command=self.register_values) button_a.pack(side=LEFT,pady=5,padx=5,expand=YES,fill=X) button_b = Button( self.root, text='Cancel', command=lambda: self.clear_entry(self.root.focus_get().cget('text'))) button_b.pack(side=LEFT,padx=5,expand=YES,fill=X) def clear_entry(self, whence): field = self.fields[int(whence[whence.index('VAR') + 3:])] self.form[field]['var'].set('') def register_values(self): for key in self.form.keys(): curfield = self.form[key] print(f"{key}: {curfield['var'].get()}") if __name__ == '__main__': MyForm()GUI image: [attachment=548] output of register button:
Output:First Name:: Harry Surname: Prudholme Sex:: M Age:: 42 Phone Number:: (555) 234-467 Email Address:: [email protected]
Thanks.