#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2

    Re-use Function Called by Event


    What is the conventional way to re-use a function that requires
    an event as an argument? If I call the function, I get the error:
    TypeError: entry_change() missing 1 required positional argument: 'event'

    There are more things going on in the the entry_change function
    than what you see in the whittled-down version below. I don't
    want to duplicate this entry_change code in the clear_all function.
    Thanks.
    Code:
    import tkinter as tk
    from tkinter import *
    
    class Test():
    
        def __init__(self, master):
            
            self.master = master
    
            self.t1 = Text(master, width=50, height=8,
                           font=('Helvetica', 18), wrap=WORD)
            self.t1.place(x=20, y=20)
    
            Label(master, text='Enter Text:',
                  font=('Helvetica', 10, 'italic')).place(x=20, y=270)
    
            self.var = StringVar()
            self.e1 = Entry(master, textvariable = self.var,
                            width=59, font=('Helvetica', 14))
            self.e1.place(x=20, y=300)
            self.e1.focus_set()
            
            self.e1.bind('<KeyRelease>', self.entry_change)
    
            btn = Button(master, text='Quit', font=('Helvetica', 15),
                         bg='Khaki4', padx=8, pady=5,
                         command=self.close_window)
            btn.place(x=20, y=350)
    
            self.clearBtn = Button(master, text='Clear All', padx=5, pady=7,
                               font=('Helvetica', 14, 'bold'), bg='DarkSlateGray2',
                               command=self.clear_all)
            self.clearBtn.place(x=220, y=350)
    
        def entry_change(self, event):
            self.t1.delete('1.0', '2.0')
            self.t1.insert('1.0', self.var.get().upper())
            
        def clear_all(self):
            self.e1.delete(0, END)
            #self.entry_change()     #raises error
            
        def close_window(self):
            self.master.destroy()
            
    def main():
        master = tk.Tk()
        master.geometry('700x425+20+20')
        app = Test(master)
        master.mainloop()
            
    if __name__=='__main__':
        main()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    522
    Rep Power
    34
    You can just pass an empty string since you don't use the event
    Code:
        def clear_all(self):
            self.e1.delete(0, END)
            self.entry_change("")     #raises error
    or call a common third function that executes the common statements from both if you want to use event to identify the key entered. Note that for the code you posted, pressing a "special" key like F1 does nothing.
    Last edited by dwblas; November 9th, 2013 at 01:04 AM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2
    Thanks dwblas for the solution that uses an empty string when calling
    an event-driven function.

    I don't understand your comment about special keys, that my code somehow prevents their use.
    Adding code to work with the the F1 key ran without error for me:
    self.master.bind('<F1>', self.call_help_function)
    Last edited by pyJer; November 9th, 2013 at 11:47 AM.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    522
    Rep Power
    34
    That was just an observation as you may not use special keys anyway. I meant that if someone enters the F1 key into the entry box (or any other "special" key combination) nothing is displayed, which again, may be what you want.
    Last edited by dwblas; November 9th, 2013 at 02:22 PM.
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Location
    California, USA
    Posts
    78
    Rep Power
    2
    Not an issue with special keys. This is an experiment in creating banner text for export to a text file.
    Each letter is capitalized and is composed of its own letter, like this:
    EEEEEEEE
    EEEEEEEE
    EE
    EE
    EE
    EEEEEE
    EEEEEE
    EE
    EE
    EE
    EEEEEEEE
    EEEEEEEE
    Currently, each letter is made up of 12 components (lines) and all stored in a dictionary.
    Dictionary keys for this letter are 'E0', 'E1'...'E11'. An idea taken from
    mainframe printout cover sheets from days of old.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    May 2009
    Posts
    522
    Rep Power
    34
    Off topic: I remember those days when we went through boxes of paper. For an into to programming course the class went to Mutual of Omaha and toured the IT department. They had one person with a low rolling cart who's only job was to restock the printers with paper from a store room the size of a small house. Ouch. Those were not the good old days in that respect.

IMN logo majestic logo threadwatch logo seochat tools logo