So I'm creating a very basic pong game in python, as one of my first independant projects, just to see what I know and to test myself. Tell me, why won't the Tkinter module work with this code?
HEIGHT=500
WIDTH=800
window=Tk()
window.title('PONG!')
c=Canvas(window,width=WIDTH,height=HEIGHT,bg='black')
c.pack()
def pongstick():
c.create_polygon(20,30, 30,30, 30,100, 20,100, fill='white')
pong1=pongstick()
MID_X = WIDTH/2
MID_Y=HEIGHT/2
c.move(pong1, MID_X, MID_Y)
This returns the following error:
Traceback (most recent call last):
File "/Users/jackstrange/Documents/Untitled.py", line 16, in <module>
c.move(pong1, MID_X, MID_Y)
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/tkinter/__init__.py", line 2430, in move
self.tk.call((self._w, 'move') + args)
_tkinter.TclError: wrong # args: should be ".4385131376 move tagOrId xAmount yAmount"
I could just be being completely stupid and forgetting something very obvious, but I don't know why this won't work!
You didn't return the ID. Try this:
def pongstick():
return c.create_polygon(20,30, 30,30, 30,100, 20,100, fill='white')
Related
I hope it run and do not stop.
I'm making Conway's Game of Life,
if my code look stupid, please help me, I'm only 11.
I use the details from wiki, if it's wrong , please tell me.
Thankyou!
import turtle,random
START_POSITION=[]
def ReStart():
global START_POSITION
#START_POSITION.clear()
y=500
x=-500
for i in range(1,26):
for a in range(1,26):
START_POSITION.append(eval(f"({x},{y})"))
x+=20
x=(0-300)
y-=20
return True
ReStart()
screen=turtle.Screen()
class Cell:
def __init__(self):
self.cells=[]
self.make_body()
self.a()
self.Alive(screen)
def make_body(self):
global START_POSITION
for i in START_POSITION:
seg=turtle.Turtle(shape="square")
seg.color("White")
seg.penup()
seg.goto(i[0],i[1])
self.cells.append(seg)
The error saids:
Traceback (most recent call last):
File "C:/Users/****/Desktop/寫程式/the life game.py", line 145, in <module>
cell=Cell()
File "C:/Users/****/Desktop/寫程式/the life game.py", line 20, in __init__
self.make_body()
File "C:/Users/****/Desktop/寫程式/the life game.py", line 29, in make_body
seg.goto(i[0],i[1])
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1777, in goto
self._goto(Vec2D(x, y))
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 3180, in _goto
self._update()
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 2661, in _update
self._update_data()
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 2647, in _update_data
self.screen._incrementudc()
File "C:\Users\****\AppData\Local\Programs\Python\Python310\lib\turtle.py", line 1293, in _incrementudc
raise Terminator
turtle.Terminator
I'm totally stuck on this,please help me.
I've reworked your code fragment into what I believe you are trying to do. Avoid eval as it can cause endless problems. Your use of global in this context isn't valid. See if this works for you:
from turtle import Screen, Turtle
class Body:
def __init__(self, positions):
self.cells = []
self.make_body(positions)
# self.a()
# self.Alive(screen)
def make_body(self, positions):
for position in positions:
cell = Turtle(shape='square')
cell.fillcolor('white')
cell.penup()
cell.goto(position)
self.cells.append(cell)
def restart():
start_positions.clear()
y = 250
for _ in range(25):
x = -250
for _ in range(25):
start_positions.append((x, y))
x += 20
y -= 20
start_positions = []
screen = Screen()
screen.setup(550, 550)
screen.bgcolor('black')
screen.tracer(False)
restart()
my_body = Body(start_positions)
screen.tracer(True)
screen.exitonclick()
Since I've turned off tracer() for speed, call screen.update() whenever you're ready for the user to see the most recent changes.
I've been writing a custom snake game using python, pygame, and pyopengl. I'm trying to draw a shape on the screen. However, I've stumbled upon this error:
Traceback (most recent call last):
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\latebind.py", line 43, in __call__
return self._finalCall( *args, **named )
TypeError: 'NoneType' object is not callable
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "F:\Projects\python\Python_Game\src\game.py", line 35, in <module>
main()
File "F:\Projects\python\Python_Game\src\game.py", line 31, in main
game.draw_shapes()
File "F:\Projects\python\Python_Game\src\game_classes.py", line 203, in draw_shapes
f.draw()
File "F:\Projects\python\Python_Game\src\game_classes.py", line 128, in draw
shape.draw()
File "F:\Projects\python\Python_Game\src\opengl_classes.py", line 128, in draw
glVertex2fv(cosine, sine)
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\latebind.py", line 47, in __call__
return self._finalCall( *args, **named )
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\wrapper.py", line 689, in wrapperCall
pyArgs = tuple( calculate_pyArgs( args ))
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\wrapper.py", line 450, in calculate_pyArgs
yield converter(args[index], self, args)
File "F:\Projects\python\Python_Game\venv\lib\site-packages\OpenGL\arrays\arrayhelpers.py", line 115, in asArraySize
byteSize = handler.arrayByteCount( result )
AttributeError: ("'NumberHandler' object has no attribute 'arrayByteCount'", <function asArrayTypeSize.<locals>.asArraySize at 0x000002642A35DCA0>)
The console is throwing me a TypeError and an Attribute error. I'm not sure if this is due to my code or an issue with one of the libraries. I'm using Python 3.9.1, pygame 2.0.1, and PyOpenGL 3.1.5.
Here's the snippet of my script where the issue arises:
class Circle:
def __init__(self, pivot: Point, radius: int, sides: int, fill: bool, color: Color):
self.pivot = pivot
self.radius = radius
self.sides = sides
self.fill = fill
self.color = color
# Draw the shape of the circle
def draw(self):
glColor3f(self.color.r, self.color.g, self.color.b)
if self.fill:
glBegin(GL_POLYGON)
else:
glBegin(GL_LINE_LOOP)
for i in range(100):
cosine = self.radius * cos(i*2*pi/self.sides) + self.pivot.x
sine = self.radius * sin(i*2*pi/self.sides) + self.pivot.y
glVertex2fv(cosine, sine)
glEnd()
The argument of glVertex2fv must be an array with 2 elements. If you have two separate coordinates which are not aggregated in an array you must use glVertex2f. See glVertex:
glVertex2fv(cosine, sine)
glVertex2f(cosine, sine)
I'm attempting to make a simple platformer game, however, I can't show the "Game Over" message because tkinder, and more specifically, tkfont, or tkinder.font, is a module, and cannot be called.
Code here. The full traceback is:
Traceback (most recent call last):
File "C:\Users\iD Student\Desktop\Connor M\Endless platformer.py", line
31, in <module>
helv36 = tkinter.font(family="Helvetica",size=36,weight="bold")
TypeError: 'module' object is not callable
tkinter.font.Font throws this traceback:
Traceback (most recent call last):
File "C:\Users\iD Student\Desktop\Connor M\Endless platformer.py", line
31, in <module>
helv36 = tkinter.font.Font(family="Helvetica",size=36,weight="bold")
File "C:\Python35\lib\tkinter\font.py", line 93, in __init__
tk.call("font", "create", self.name, *font)
AttributeError: 'NoneType' object has no attribute 'call'
which I assume to be an error in tkinter itself. Relevant code:
import tkinter
from tkinter.font import *
helv36 = tkinter.font.Font(family="Helvetica",size=36,weight="bold")
def draw_text(display_string, font, surface, x_pos, y_pos):
text_display = font.font(display_string, 1, (0, 0, 0))
surface.blit(text_display, (x_pos, y_pos))
#Ends the game if the player dies
if y >640:
endgame = True
if endgame:
draw_text("GAME OVER", helv36, screen, 50, 50)
You can't create a font until after you've created a root window.
I don't know why this is giving me an attribute error. I want my blah() function to shuffle the cards. I'm calling the builtin function shuffle() from random.
Error:
Exception in Tkinter callback
Traceback (most recent call last):
File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1489, in __call__
return self.func(*args)
File "gui1.py", line 105, in blah
shuffle(cards)
AttributeError: Button instance has no __call__ method
Here's the code snippet:
def blah():
global card_count
global path
shuffle(cards)
card_count = 0
path = generate_paths(cards)
print "Cards Shuffled"
shuffle = Button(frame_buttons, text = "SHUFFLE",height = 2, width = 10,command =blah)
shuffle.grid(row = 2 , padx = 40, pady = 40)
shuffle is the name of the function in random. However, it's also the name of the Button. Change the Button's name to something like shuffle_button and you should be fine.
My program gives me this error:
Traceback (most recent call last):
File "C:\Simulation\SymDialog.py", line 153, in OnPaint
self.Redraw(False)
File "C:\Simulation\SymDialog.py", line 173, in Redraw
obj.Draw(self.dc)
File "C:\Simulation\SymDialog.py", line 207, in Draw
dc.DrawCircle(self._x, self._y, self._r)
File "E:\Python\Python27\lib\site-packages\wx-2.8-msw-unicode\wx\_gdi.py", line 3391, in DrawCircle
return _gdi_.DC_DrawCircle(*args, **kwargs)
OverflowError: Python int too large to convert to C long
Method from error:
OnPaint
def OnPaint(self, event):
self.Redraw(False)
wx.GCDC(wx.BufferedPaintDC(self, self._buffer))
event.Skip()
Redraw
def Redraw(self, clear):
if clear == True: del self.drawList[:]
self.dc = wx.GCDC(wx.BufferedDC(None, self._buffer))
self.dc.SetBackground(wx.Brush(self.GetBackgroundColour()))
self.dc.Clear()
for obj in self.drawList:
obj.Draw(self.dc)
del self.dc
Draw
def Draw(self, dc):
self.setDC(dc)
dc.DrawCircle(self._x, self._y, self._r)
How can I fix this error ?
Thanks for answers
You might be mistakenly using too large values.
Edit: Since I can't comment, what are the sizes of self._x, self._y, and self._r?