AttributeError: 'function' object has no attribute 'geometry' - python

I've created a toplevel window in my Tkinter program. However it just doesn't seem to get the geometry part.
Some of the code:
def calreco():
calreco_screen = Toplevel(cc)
calreco_screen.title("Your Recommended Calorie Intake Is")
calreco.geometry("400x400")

You are initiated and called a different/unknown object.
calreco.geometry("400x400")
Instead, Try
calreco_screen.geometry("400x400")
This could solve your issue.

Related

Some turtle functions not working on new turtle

So i have created a new turtle by doing bassel = turtle.Turtle(), however, some functions such as tracer() and onkeypress() just don't work, I get an error saying 'Turtle' object has no attribute 'tracer' or 'Turtle' object has no attribute 'onkeypress'...
But as soon as I replace bassel by turtle it works
So for instance, bassel.onkeypress() doesn't work ('Turtle' object has no attribute 'onkeypress')
But if I put turtle.onkeypress(), it works perfectly.
Here's some part of the code
bassel = turtle.Turtle()
bassel.tracer(0)
bassel.hideturtle()
Right. Those functions are not attributes of a SPECIFIC turtle object, they are services offered by the turtle module. Just use turtle.tracer and turtle.onkeypress.

AttributeError: 'PySide2.QtCore.Qt.Alignment' object has no attribute 'testFlag'

I am using Pyside2. I get the error
AttributeError: 'PySide2.QtCore.Qt.Alignment' object has no attribute 'testFlag'
for this piece of code
if (self.m_alignment.testFlag(Qt.AlignTop)):
The alignment is defined thus:
m_alignment = Qt.AlignTop | Qt.AlignRight
Is there something I am doing wrong? Or is there another way to check for an alignment flag in PySide2? Been stuck at this for some time.

I am using a function to set my app at centre of the screen but it is giving me error 'builtin_function_or_method' object has no attribute 'center'

It is showing : 'builtin_function_or_method' object has no attribute 'center'.
code is here:
def setCenter(self): ///this function not working
'''Function to align the application at centre'''
qRect=self.frameGeometry()
centerPoint=QDesktopWidget().availableGeometry.center()
qRect.moveCenter(centerPoint)
self.move(qRect.topLeft())
Also, I have named my application window as mywindow, so I am calling this function as mywindow.setCenter() in main().
Am I doing it right ?
QDesktopWidget.availableGeometry is a function. Therefore it doesn't have an attribute of center. You need to call the function like availableGeometry(). The returned QRect has a center attribute you can call.
For the geometry of the default screen just use:
QDesktopWidget().availableGeometry().center()

Python Tkinter call a dictionary with a StringVar?

I'm working with tkinter in python and I have an annoying bug that I can't seem to fix, although the answer is probably obvious.
I am trying to call a dictionary with a string, but for some reason I get the error:
Type Error: unhashable type: StringVar. Here is a snippet of code with that issue:
from Tkinter import *
gpa = Tk()
weightsy = {'0': 2, '.1': 6, '.25':.2, '.5':.56, '.75':75, '1':12}
acadw = StringVar()
acadw.set(".1")
print (weightsy.get(acadw)) #Here is the issue; it should return the integer 6.
mainloop()
For extra info, if I remove the tkinter related code (such as the import, gpa = Tk(), StringVar, .set, mainloop()) it works, so I believe it is a tkinter related issue.
Just as you had to call set method of StringVar object, you also need to call get to retrieve the str data.
print weightsy[acadw.get()]
The dictionary doesn't know to convert your object into a string so it attempts to get the value associated with acadw. You get a TypeError rather than a KeyError since StringVar objects happen to be unhashable.

Binding MouseClick to widget of an instance

I have an instance of a ttkcalendar object, "cal". When I bind a button click to cal, the function called only executes if I click in the corners of the ttcalendar frame; when I click on the actual calendar area, while the ttkcalendar functions execute, my bind does not.
This code runs when I click Frame corners
# Calendar Frame
self.cal=Calendar(LeftFrame)
self.cal.pack(side=TOP)
self.cal.bind("<Button-1>",self.clicked)
I thought that if I try to bind to the canvas object of the calendar it would work. However this code returns AttributeError: Calendar instance has no attribute canvas.
# Calendar Frame
self.cal=Calendar(LeftFrame)
self.cal.pack(side=TOP)
self.cal.canvas.bind("<Button-1>",self.clicked)
As I said, internal ttkcalendar binds work fine for switching date shown. Any insights? Thanks
The error message should be pretty clear. If python is telling you "AttributeError: Calendar instance has no attribute canvas", you must assume that to be true.
Looking at the source code of the Calendar class, I don't see any canvas attribute. Just like the error is telling you, you're trying to access an attribute that doesn't exist.
The Calendar class does have an attribute named _canvas, maybe you can try using that instead. Though, that leading underscore denotes that it is intended as a private attribute and may go away in future revisions of the code.

Categories