November 8th, 2013, 10:11 PM
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.
import tkinter as tk
from tkinter import *
def __init__(self, master):
self.master = master
self.t1 = Text(master, width=50, height=8,
font=('Helvetica', 18), wrap=WORD)
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))
btn = Button(master, text='Quit', font=('Helvetica', 15),
bg='Khaki4', padx=8, pady=5,
self.clearBtn = Button(master, text='Clear All', padx=5, pady=7,
font=('Helvetica', 14, 'bold'), bg='DarkSlateGray2',
def entry_change(self, event):
#self.entry_change() #raises error
master = tk.Tk()
app = Test(master)
November 9th, 2013, 12:01 AM
You can just pass an empty string since you don't use the event
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.
self.entry_change("") #raises error
Last edited by dwblas; November 9th, 2013 at 12:04 AM.
November 9th, 2013, 10:06 AM
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:
Last edited by pyJer; November 9th, 2013 at 10:47 AM.
November 9th, 2013, 11:55 AM
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 01:22 PM.
November 9th, 2013, 01:29 PM
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:
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.
November 9th, 2013, 03:49 PM
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.