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'.
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?
On Fri, 17 Jun 2022, MRAB wrote:
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'.
Haven't I done this in the application class?
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 not, where do I specify the instance of the ContactNameInput class?
This:
self.inputs['Last name'] = cc.LabelInput(
ContactNameInput, 'lname',
input_class = ttk.Entry,
input_var = tk.StringVar()
)
should be this:
self.inputs['Last name'] = cc.LabelInput(
self, 'lname',
input_class = ttk.Entry,
input_var = tk.StringVar()
)
Also, this:
self.inputs['First name'] = cc.LabelInput(
ContactNameInput, 'fname',
input_class = ttk.Entry,
input_var = tk.StringVar()
)
should be this:
self.inputs['First name'] = cc.LabelInput(
self, 'fname',
input_class = ttk.Entry,
input_var = tk.StringVar()
)
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):
ContactNameInput, 'lname',
ContactNameInput, 'fname',
ContactNameInput(self).grid(row = 0, column = 0, sticky=('EWNS'))
ContactNameInput, 'lname',
ContactNameInput, 'fname',
On Fri, 17 Jun 2022, Dennis Lee Bieber wrote:
ContactNameInput, 'lname',
ContactNameInput, 'fname',
This works if a tk.labelframe is where the widget is placed. In my case, as MRAB taught me, the proper syntax is
self,'lname'...
self.'fname'...
There is a comma (U+002C) here ...
And a dot (U+002E) here.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 285 |
Nodes: | 16 (2 / 14) |
Uptime: | 61:13:43 |
Calls: | 6,488 |
Calls today: | 1 |
Files: | 12,094 |
Messages: | 5,274,431 |