Tk window returns AttributeError: __len__ - python

Here I'm starting to create a TK window with a text field but when ever I run this I get the error
Exception in Tkinter callback
Traceback (most recent call last):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1470, in __call__
return self.func(*args)
File "School.py", line 31, in begin
emulatorI=emulator()
File "School.py", line 20, in __init__
code.pack(self.root)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1868, in pack_configure
+ self._options(cnf, kw))
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1105, in _options
cnf = _cnfmerge(cnf)
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 114, in _cnfmerge
for c in _flatten(cnfs):
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/lib-tk/Tkinter.py", line 1826, in __getattr__
return getattr(self.tk, attr)
AttributeError: __len__
from within the module. What am I doing wrong? I'm realitively new to classes so I may have done something wrong in the class setup.
I'm running python 2.7 on OS 10.9
class emulator:
def __init__(self):
self.root=Tk()
self.root.geometry("500x500")
self.root.title("Python")
code=Text(self.root)
code.pack(self.root)
self.root.mainloop()
emulatorI=emulator()

The problem is with the line
code.pack(self.root)
The pack function normally takes no arguments except keyword arguments, and so should just be called as
code.pack()
The reason for the very odd error is that pack can take a positional argument, which is expected to be a dictionary of options. When trying to treat the Tk instance as a dictionary, it failed due to the lack of a __len__ method.

What is the purpose of code.pack(self.root)?
The arguments for pack are placement directions, such as side, padding, fill, anchor, etc
(see http://effbot.org/tkinterbook/pack.htm ).

Related

tkinter picture error: bad screen distance

I'm trying to write a tkinter program, and I came across an error that I've never seen before while working with tkinter. I've searched around everywhere, and tried everything I can think of. This is my code so far:
x=tk.Canvas(top,width=1000,height=750,bg="grey")
x.pack()
y=tk.PhotoImage(file="C:\\Users\\Admin\\Desktop\\images (3)_CnyokaDvJmG1xu.png")
x.create_image(top,0,0,image=y)`
and this is error
Traceback (most recent call last):
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
1705, in __call__
return self.func(*args)
File "C:/Users/Admin/.spyder-py3/temp.py", line 16, in open
x.create_image(top,0,0,image=y).pack()
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
2489, in create_image
return self._create('image', args, kw)
File "C:\Users\Admin\Anaconda3\lib\tkinter\__init__.py", line
2480, in _create
*(args + self._options(cnf, kw))))
_tkinter.TclError: bad screen distance "."
You don't have to supply containing widget when placing an image in a canvas, coordinates are enough:
x.create_image(0,0,image=y)
The error is because canvas does not accept a widget as coordinates.

Cannot override custom field __init__ and make migrations, throws error

I was attempting to create a custom model field and use it in a model as the docs demonstrate for BetterCharField shown here (you might have to scroll a bit down): https://docs.djangoproject.com/en/3.0/howto/custom-model-fields/#custom-database-types
My code in my models.py is as follows, almost verbatim from the docs example:
from django.db import models
# Create your models here.
class BetterCharField(models.Field):
def __init__(self, max_length, *args, **kwargs):
self.max_length = max_length
super().__init__(*args, **kwargs)
def db_type(self, connection):
return "char({})".format(self.max_length)
class MyModel(models.Model):
my_field = BetterCharField(25)
However, when trying to run python manage.py makemigrations with this models.py file, I get the following error every time:
Traceback (most recent call last):
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/db/migrations/state.py", line 413, in from_model
fields.append((name, field.clone()))
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/db/models/fields/__init__.py", line 512, in clone
return self.__class__(*args, **kwargs)
TypeError: __init__() missing 1 required positional argument: 'max_length'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "manage.py", line 21, in <module>
main()
File "manage.py", line 17, in main
execute_from_command_line(sys.argv)
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 401, in execute_from_command_line
utility.execute()
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/core/management/__init__.py", line 395, in execute
self.fetch_command(subcommand).run_from_argv(self.argv)
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/core/management/base.py", line 328, in run_from_argv
self.execute(*args, **cmd_options)
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/core/management/base.py", line 369, in execute
output = self.handle(*args, **options)
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/core/management/base.py", line 83, in wrapped
res = handle_func(*args, **kwargs)
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/core/management/commands/makemigrations.py", line 142, in handle
ProjectState.from_apps(apps),
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/db/migrations/state.py", line 221, in from_apps
model_state = ModelState.from_model(model)
File "/Users/dark_knight/test/venv/lib/python3.7/site-packages/django/db/migrations/state.py", line 418, in from_model
e,
TypeError: Couldn't reconstruct field my_field on polls.MyModel: __init__() missing 1 required positional argument: 'max_length'
Why is this failing? I had yet to create any migrations at all for this project, let alone the field and model given here. I have my positional argument as 25 given to BetterCharField when I initialize it in my models.py file. What am I missing? I had a much more complex use case, but I built this example in a brand new test django project/app straight from the documentation, and it still fails.
This is documented behaviour:
You can’t modify the number of positional arguments in an already migrated custom field without raising a TypeError. The old migration will call the modified __init__ method with the old signature. So if you need a new argument, please create a keyword argument and add something like assert 'argument_name' in kwargs in the constructor.

Weird error I receive from Tkinter in Python

I am attempting to make a tkinter project but as I tried to run for like the third time it gives me this error:
Traceback (most recent call last):
File "/Users/cool/Documents/STM Wisepay Service.py", line 63, in <module>
app = App(root)
File "/Users/cool/Documents/STM Wisepay Service.py", line 20, in __init__
self.create_buttons()
File "/Users/cool/Documents/STM Wisepay Service.py", line 30, in create_buttons
tk.Button(button_frame, text = "Add to Debt", commmand = self.debt).grid(column = 6, row = 5)
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2366, in __init__
Widget.__init__(self, master, 'button', cnf, kw
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/tkinter/__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf)
_tkinter.TclError: unknown option "-commmand"
What can I do to fix this? What are the problems with my code?
Here is my code:
https://pastebin.com/mWPEFbpz
You are spelling command wrong (three m's)
You are passing it a variable called commmand and it doesn't know how to use that variable.
tk.Button(... commmand = ...
You can see it in the error message right here
_tkinter.TclError: unknown option "-commmand"
These python error messages are actually very helpful. I would encourage you to read them carefully to catch stuff like this.

Unable to access a declared image by imageTk within a definition

I have declared an image file within a class and I'm able to show it in the class body. But when I am calling it within a function to show it within the function body it throws an error.
Here is my code:
import tkinter
from tkinter import *
from PIL import Image,ImageTk
body=Tk()
ftpim = Image.open('textfile_big.png')
ftpfileimage = ImageTk.PhotoImage(ftpim)
lbl=Label(body,image=ftpfileimage).pack()
def nextimg():
cbody=Tk()
lbl=Label(cbody,image=ftpfileimage).pack()
but=Button(body,command=nextimg,text="show").pack()
body.mainloop()
When I'm executing it, however, I have the error "pyimage1" doesn't exist :
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\My Love\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 1702, in __call__
return self.func(*args)
File "C:\Users\My Love\Desktop\New Folder\Unknown_Project\Unknown_package\sssss.py", line 10, in nextimg
lbl=Label(cbody,image=ftpfileimage).pack()
File "C:\Users\My Love\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2763, in __init__
Widget.__init__(self, master, 'label', cnf, kw)
File "C:\Users\My Love\AppData\Local\Programs\Python\Python37-32\lib\tkinter\__init__.py", line 2296, in __init__
(widgetName, self._w) + extra + self._options(cnf))
_tkinter.TclError: image "pyimage1" doesn't exist

AttributeError: Menu instance has no attribute '__len__'

I am not familiar with the ways of python, i saw few other questions here with similar description, but could not fix this.
Error:
Traceback (most recent call last):
File "C:/Users/UT/PycharmProjects/tkinter/python_PET/main.py", line 16, in <module>
m = menu_bar_class(root)
File "C:/Users/UT/PycharmProjects/tkinter/python_PET/main.py", line 14, in __init__
self.master.config(self.menu)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1326, in configure
return self._configure('configure', cnf, kw)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1312, in _configure
cnf = _cnfmerge(cnf)
File "C:\Python27\lib\lib-tk\Tkinter.py", line 114, in _cnfmerge
for c in _flatten(cnfs):
AttributeError: Menu instance has no attribute '__len__'
Program:
from Tkinter import *
from tkFileDialog import *
import tkMessageBox
import ttk
root = Tk()
class menu_bar_class:
def __init__(self,master):
self.master = master
print("menu bar")
self.menu = Menu(self.master)
self.master.config(self.menu)
m = menu_bar_class(root)
root.mainloop()
You need to pass in the menu as a keyword argument:
self.master.config(menu=self.menu)
When you pass in a positional argument (so without the menu= part), then Tkinter expects to receive either a dictionary with configuration (so {'menu': self.menu}) or a sequence containing more sequences or dictionaries. Because self.menu is neither, you get the error you see.

Categories

Resources