Python Forum
[Tkinter] How to get & delete details from each input by clicking a button
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] How to get & delete details from each input by clicking a button
#1
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.
Reply
#2
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.
Reply
#3
(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.
Reply
#4
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]
Reply
#5
(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.
Reply
#6
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()
Reply
#7
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Scrollable buttons with an add/delete button Clich3 5 3,332 Jun-16-2022, 07:19 PM
Last Post: rob101
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,197 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  Need tkinter help with clicking buttons pythonprogrammer 2 2,400 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 4,949 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] how to input a random entry with each button press? nadavrock 1 6,330 Jun-17-2019, 05:28 AM
Last Post: Yoriz
  [Tkinter] RE: status bar to return to the centre after 1 minute of clicking a button ? chano 6 4,609 May-27-2019, 04:24 PM
Last Post: Yoriz
  tkinter- adding a new window after clicking a button built on the gui ShashankDS 2 6,546 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] Adding New TAB to NoteBook Widget by Clicking Vicolas 0 2,583 Feb-15-2019, 06:03 PM
Last Post: Vicolas
  [Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return Vicolas 1 5,101 Feb-02-2019, 01:53 AM
Last Post: woooee
  [PyQt] No reaction and no error message when clicking button Atalanttore 4 4,749 Nov-23-2018, 01:48 PM
Last Post: Atalanttore

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020