Tkinter module test: widget class not inserted in application frame
MRAB
python at mrabarnett.plus.com
Fri Jun 17 12:47:24 EDT 2022
On 2022-06-17 17:19, Rich Shepard wrote:
> I'm not seeing the error source in a small tkinter module I'm testing.
>
> The module code:
> -----------
> import tkinter as tk
> from tkinter import ttk
>
> import common_classes as cc
>
> class ConactNameInput(tk.Frame):
> def __init__(self, parent, *args, **kwargs):
> super().__init__(parent, *args, **kwargs)
> # A dict to keep track of input widgets
> self.inputs = {}
>
> self.inputs['Last name'] = cc.LabelInput(
> ContactNameInput, 'lname',
> input_class = ttk.Entry,
> input_var = tk.StringVar()
> )
> self.inputs['Last name'].grid(row = 0, column = 0)
> #
> self.inputs['First name'] = cc.LabelInput(
> ContactNameInput, 'fname',
> input_class = ttk.Entry,
> input_var = tk.StringVar()
> )
> self.inputs['First name'].grid(row = 0, column = 1)
>
> okay_button = tk.Button(self, text="OK",
> command=self.ok)
> okay_button.pack(side=tk.LEFT, padx=(20, 0), pady=(0, 20))
>
> cancel_button = tk.Button(self, text="Cancel",
> command=self.cancel)
> cancel_button.pack(side=tk.RIGHT, padx=(0, 20), pady=(0, 20))
>
> def okay_button(self):
> pass
>
> def cancel_button(self):
> Quitter()
>
> def get_last_name(self):
> pass
>
> def get_first_name(self):
> pass
>
>
> class NameApplication(tk.Tk):
> def __init__(self, *args, **kwargs):
> super().__init__(*args, **kwargs)
> self.title("Contact Name")
> self.geometry("800x600")
> self.resizable(width = False, height = False)
> ContactNameInput(self).grid(row = 0, column = 0, sticky=('EWNS'))
> self.columnconfigure(0, weight=1)
>
> if __name__ == '__main__':
> app = NameApplication()
> app.mainloop()
> ----------
>
> The python error traceback:
> ----------
> Traceback (most recent call last):
> File "contact_history_name_input.py", line 60, in <module>
> app = NameApplication()
> File "contact_history_name_input.py", line 55, in __init__
> ContactNameInput(self).grid(row = 0, column = 0, sticky=('EWNS'))
> File "contact_history_name_input.py", line 17, in __init__
> input_var = tk.StringVar()
> File "/home/rshepard/development/BusinessTracker/views/common_classes.py", line 46, in __init__
> super().__init__(parent, **kwargs)
> File "/usr/lib64/python3.7/tkinter/__init__.py", line 2744, in __init__
> Widget.__init__(self, master, 'frame', cnf, {}, extra)
> File "/usr/lib64/python3.7/tkinter/__init__.py", line 2292, in __init__
> BaseWidget._setup(self, master, cnf)
> File "/usr/lib64/python3.7/tkinter/__init__.py", line 2262, in _setup
> self.tk = master.tk
> AttributeError: type object 'ContactNameInput' has no attribute 'tk'
> ----------
>
> I'm not correctly placing the NameInput class in the NameApplication frame.
> What have I missed?
>
You haven't shown the code for common_classes.LabelInput, but I'm
guessing that the first argument should be the parent.
You're passing in the _class_ ConactNameInput, but I'm guessing that it
should be an _instance_ of that class, in this case, 'self'.
More information about the Python-list
mailing list