Posts: 20
Threads: 11
Joined: Jan 2019
from tkinter import *
import Pmw
root = Tk()
root.title('EntryField')
var = StringVar()
for i in ['First Name:','Surname','Sex:','Age:','Phone Number:','Email Address:']:
entryfield = Pmw.EntryField(root,labelpos=W,label_text=i,label_width=15,entry_width=24,entry_textvariable=var)
entryfield.pack(side=TOP,padx=5,pady=5)
button = Button(root,text='Register',command=lambda: print(var.get()))
button.pack(side=LEFT,pady=5,padx=5,expand=YES,fill=X)
button = Button(root,text='Cancel',command=lambda:entryfield.delete(END,var.get()))
button.pack(side=LEFT,padx=5,expand=YES,fill=X) Hi, please how can I click on the 'Register' button and get all the information inputed into each entryfield.
Also, how can I delete each and every inputed information in the entryfield by clicking on the 'Cancel' button?
And again, please why am I tryping in one entryfield and getting it duplicated on the others. Is there how I can stop it?
Thanks.
Posts: 12,030
Threads: 485
Joined: Sep 2016
You have some issues with your code.
you create 6 entry fields, but assign the text entered of each to the same entry field. They are all assigned to the same variable 'var'. This accounts for the duplication in each entry.
Also, although legally valid, the assignment of button to print statement is odd.
Usually, you would assign the button to a function that would process the contents of fields.
I'll play with this a bit.
Posts: 20
Threads: 11
Joined: Jan 2019
(Jan-30-2019, 08:54 PM)Larz60+ Wrote: You have some issues with your code. you create 6 entry fields, but assign the text entered of each to the same entry field. They are all assigned to the same variable 'var'. This accounts for the duplication in each entry. Also, although legally valid, the assignment of button to print statement is odd. Usually, you would assign the button to a function that would process the contents of fields. I'll play with this a bit. Please how can I go about all these? Thanks.
Posts: 12,030
Threads: 485
Joined: Sep 2016
Jan-30-2019, 11:15 PM
(This post was last modified: Jan-31-2019, 05:26 PM by Larz60+.)
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:
output of register button:
Output: First Name:: Harry
Surname: Prudholme
Sex:: M
Age:: 42
Phone Number:: (555) 234-467
Email Address:: [email protected]
Posts: 20
Threads: 11
Joined: Jan 2019
(Jan-30-2019, 11:15 PM)Larz60+ Wrote: 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]
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.
Thanks.
Posts: 12,030
Threads: 485
Joined: Sep 2016
Jan-31-2019, 09:55 PM
(This post was last modified: Jan-31-2019, 09:56 PM by Larz60+.)
Quote: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.
Thanks.
That's simple, you see how to access each entry field by looking at 'resister_values' method.
just add another button 'button_c'
and a clear_all method
button_c = Button(
self.root,
text = 'Clear All',
command=self.clear_all)
button_c.pack(side=LEFT,padx=5,expand=YES,fill=X)
def clear_all(self):
for key in self.form.keys():
self.form[key]['var'].set('') so new code becomes:
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)
button_c = Button(
self.root,
text = 'Clear All',
command=self.clear_all)
button_c.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 clear_all(self):
for key in self.form.keys():
self.form[key]['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()
Posts: 20
Threads: 11
Joined: Jan 2019
(Jan-31-2019, 09:55 PM)Larz60+ Wrote: Quote: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. Thanks.
That's simple, you see how to access each entry field by looking at 'resister_values' method. just add another button 'button_c' and a clear_all method button_c = Button( self.root, text = 'Clear All', command=self.clear_all) button_c.pack(side=LEFT,padx=5,expand=YES,fill=X) def clear_all(self): for key in self.form.keys(): self.form[key]['var'].set('') so new code becomes: 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) button_c = Button( self.root, text = 'Clear All', command=self.clear_all) button_c.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 clear_all(self): for key in self.form.keys(): self.form[key]['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() Thanks Sir.
|