Python Forum
[Tkinter] define entry via class on a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] define entry via class on a loop
#4
What don't you understand about "super().__init__(parent, *args, bg=bg, **kwargs)"? Is this a "I don't understand subclassing" question or a "I don't understand super()" question or a "I don't understand this particular argument" question?

So your Entry class should have been named DictionaryEntry. I am going to assume that you want to use the DictionaryEntry class to display/edit an entry in a dictionary. DictionaryEntry has a label that displays the key name and a tk.Entry that displays/edits the value. When you change the value in the tk.Entry the DictionaryEntry object updates the entry value in the dictionary.

If that is correct, something like this should work.
import tkinter as tk
from tkinter import messagebox

class DictionaryEntry(tk.Frame):
    '''Use me to display/edit a dictionary entry.  Change entry value by typing in the Entry
    control and pressing <Return> or changing focus.
    
    I retain the type of the entry value.  If the initial value is an int, I verify the Entry value
    is an int and display a message if it is not.
    '''
    def __init__(self, parent, dictionary, key, bg=None, **kwargs):
        if bg is None:
            bg = parent['bg'] 
        super().__init__(parent, bg=bg, **kwargs)
        self.dictionary = dictionary
        self.key = key
        value = dictionary[key]
        self.type = type(value)
        tk.Label(self, text=key, width=10).grid(row=0, column=0, sticky='E')
        self.variable = tk.StringVar(self, str(value))
        entry = tk.Entry(self, textvariable=self.variable, width=10)
        entry.grid(row=0, column=1)
        entry.bind('<Return>', self._accept)
        entry.bind('<FocusOut>', self._accept)

    def _accept(self, event):
        '''Accept value change when return is pressed or losing focus'''
        try:
            value = self.type(self.variable.get())
            self.dictionary[self.key] = value
        except ValueError:
            messagebox.showinfo('Value Error', f'{self.variable.get()} is not a {self.type.__name__}')
        self.variable.set(str(self.dictionary[self.value]))           

if __name__ == '__main__':
    dictionary = {'A':1, 'B':2.0, 'C':'string'}

    root = tk.Tk()
    for key in dictionary:
        DictionaryEntry(root, dictionary, key).pack(padx=10, pady=5)
    tk.Button(root, text="click me", command=lambda: print(dictionary)).pack(padx=10, pady=5)
    root.mainloop()
Reply


Messages In This Thread
define entry via class on a loop - by drSlump - Oct-19-2021, 07:45 AM
RE: define entry via class on a loop - by drSlump - Oct-20-2021, 07:05 AM
RE: define entry via class on a loop - by deanhystad - Oct-20-2021, 08:08 PM
RE: define entry via class on a loop - by drSlump - Oct-21-2021, 11:51 AM
RE: define entry via class on a loop - by drSlump - Oct-21-2021, 03:24 PM
RE: define entry via class on a loop - by drSlump - Oct-27-2021, 03:09 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Binding Entry box to <Button-3> created in for loop iconit 5 7,176 Apr-22-2020, 05:47 AM
Last Post: iconit
  Transfer Toplevel window entry to root window entry with TKinter HBH 0 5,249 Jan-23-2020, 09:00 PM
Last Post: HBH
  [Tkinter] Setting Binding to Entry created with a loop? p_hobbs 1 2,652 Nov-25-2019, 10:29 AM
Last Post: Larz60+
  [Tkinter] how to get the entry information using Entry.get() ? SamyPyth 2 4,305 Mar-18-2019, 05:36 PM
Last Post: woooee
  [Tkinter] Bringing function out of class into main loop zukochew 1 3,198 Jul-30-2018, 06:43 PM
Last Post: Axel_Erfurt
  How to define a method in a class 1885 2 5,498 Oct-29-2017, 02:00 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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