Some turtle functions not working on new turtle - python

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.

Related

How to stop turtle(python) from giving me this error: 'int' object has no attribute 'undobuffer'?

How can i stop turtle from returning this error?
'int' object has no attribute 'undobuffer'
import turtle
import time
import math
t=turtle.Turtle
def petalo():
t.circle(250,50)
t.circle(250,-50)
petalo()
´´´
t=turtle.Turtle does not instantiate an instance of object Turtle. As per #jasonharper's comment above, you need to add the parenthesis after and things will work.
t = turtle.Turtle()
t.circle(250,50)
...

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

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.

What is a Turtle Object?

The book i'm reading is called Think Python 2. The author keeps on mentioning objects but i still do not understand what they actually are.... The code reads as follows
import turtle
bob = turtle.Turtle()
print(bob)
turtle.mainloop()
"The turtle module (with a lowercase ’t’) provides a function called Turtle (with an uppercase ’T’) that creates a Turtle object."
So what this means is that the module defined the function Turtle, and when it was defined it created a function object 'Turtle'?
"which we assign to a variable named bob. Printing bob displays something like
turtle.Turtle object at 0xb7bfbf4c
This means that bob refers to an object with type Turtle as defined in module turtle."
I can't understand what is he doing atm... Is he assigning the return value of Turtle()function to a variable called Bob?
And why is bob's type Turtle? Isn't it type function? As when you define a function it creates a function object in this case "Turtle" of type "Function"...
I'm kinda messed up. What am i missing?
# Import the turtle module
import turtle
# Create a variable named bob, assign it a Turtle object which comes from the turtle module
bob = turtle.Turtle()
# Print out the variable bob
print(bob)
# Call a method named mainloop from the turtle package
turtle.mainloop()
Read here for more about what a module is.
An object is sort of like a blueprint for a code-like thing. When the author of the book does
bob = turtle.Turtle()
They are "instantiating" (or creating) a specific version of the Turtle object, which you will refer to as bob. Bob has certain methods you can call on it, which all turtle objects have, but when you do something like.
bob.forward(100)
It will move your specific turtle forward by 100 units.

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()

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