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
#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


Messages In This Thread
RE: How to get & delete details from each input by clicking a button - by Larz60+ - Jan-30-2019, 11:15 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Scrollable buttons with an add/delete button Clich3 5 3,666 Jun-16-2022, 07:19 PM
Last Post: rob101
  [Tkinter] Clicking on the button crashes the TK window ODOshmockenberg 1 2,341 Mar-10-2022, 05:18 PM
Last Post: deanhystad
  Need tkinter help with clicking buttons pythonprogrammer 2 2,553 Jan-03-2020, 04:43 AM
Last Post: joe_momma
  [PySimpleGui] How to alter mouse click button of a standard submit button? skyerosebud 3 5,140 Jul-21-2019, 06:02 PM
Last Post: FullOfHelp
  [Tkinter] how to input a random entry with each button press? nadavrock 1 6,556 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,875 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,771 Apr-18-2019, 12:48 PM
Last Post: ShashankDS
  [Tkinter] Adding New TAB to NoteBook Widget by Clicking Vicolas 0 2,686 Feb-15-2019, 06:03 PM
Last Post: Vicolas
  [Tkinter] Clicking a RadioButton in a for Loop & Getting the Appropriate Return Vicolas 1 5,256 Feb-02-2019, 01:53 AM
Last Post: woooee
  [PyQt] No reaction and no error message when clicking button Atalanttore 4 4,920 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