First of all, take a look at my previous thread here: Tkinter understanding mainloop
After following the advice from there, in GUI programming, infinite loops have to be avoided at all costs, in order to keep the widgets responsive to user input.
Instead of using:
while 1:
ball.draw()
root.update()
time.sleep(0.01)
I managed using self.canvas.after(1, self.draw) inside my draw() function.
So my code now looks like this:
# Testing skills in game programming
from Tkinter import *
root = Tk()
root.title("Python game testing")
root.resizable(0, 0)
root.wm_attributes("-topmost", 1)
canvas = Canvas(root, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
root.update()
class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
self.canvas_height = canvas.winfo_height()
self.x = 0
self.y = -1
def draw(self):
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 1
if pos[3] >= self.canvas_height:
self.y = -1
self.canvas.after(2, self.draw)
ball = Ball(canvas, "red")
ball.draw()
root.mainloop()
However, time inside self.canvas.after() does not work properly...
If set to 1 it's extremely fast! If it's set to 10, 5 or even 2, it's too slow!
I didn't have that problem while using the above while loop in my code since the time.sleep() worked as it should!
EDIT:
I can now report that the time inside the after function of Tkinter does not work properly in my Windows 8.1 tablet, and in my Windows 8.1 laptop, while in the same laptop when running Ubuntu through virtual machine it does work as it should.
Time in time.sleep is in seconds whereas in after() it is in milliseconds. time.sleep(0.01) is the same as self.canvas.after(10, self.draw). If after(2, func) was too slow but after(1, func) was too fast then you could try sleep(0.0005) then after(1, func) do give a delay of 1.5 milliseconds but it is barely noticeable. Either way, fiddle with the timing until the pause is right.
Speed of the object (canvas) and clock/loop speed should be considered as two different things, IMHO.
Hence, you can leave loop after 2ms or even larger like 10...25...
...
self.canvas.after(2, self.draw)
... # loop this after 2 ms.
while, changing speed in terms of 'how many pixels' :
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 20
if pos[3] >= self.canvas_height:
self.y = -20
so adjusting these values, and :
self.canvas.move(self.id, 245, 100)
can let you fine-tune position and speed of your red dot.
Hope it helps
Related
Here is my code but for some reason whenever I run it the ball only goes in one direction.
from tkinter import * import random import time
class Ball:
def __init__(self, canvas, color):
self.canvas = canvas
self.id = canvas.create_oval(10, 10, 25, 25, fill=color)
self.canvas.move(self.id, 245, 100)
starts = [-3, -2, -1, 1, 2, 3]
random.shuffle(starts)
self.x = starts[0]
self.y = -3
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
def draw(self):
self.canvas.move(self.id, self.x, self.y)
pos = self.canvas.coords(self.id)
if pos[1] <= 0:
self.y = 1
if pos[3] >= self.canvas_height:
self.y = -1
if pos[0] <= 0:
self.x = 3
if pos[2] >= self.canvas_width:
self.x = -3
tk = Tk()
tk.title("Game")
tk.resizable(0, 0)
tk.wm_attributes("-topmost", 1)
canvas = Canvas(tk, width=500, height=400, bd=0, highlightthickness=0)
canvas.pack()
ball = Ball(canvas, 'red') while 1:
ball.draw()
tk.update_idletasks()
tk.update()
time.sleep(0.01)
Can someone please help me?
Shuffle is working properly but there is some problem with your logic due to which ball always go to the left side and in the upward direction. You need to revise that. Also you must use if and elif in draw method for both x and y to stop your ball going out of the frame instead of using if for all.
Feel free to ask question incase of any confusion.
In case you ever run into issue like this, try debugging or at least printing some crucial values to double-check if these in fact are correct (same as you assumed).
And so if you would print self.canvas_height and self.canvas_width values you would notice that these are incorrect (they always return 1).
Solution to your problem is to change these lines in your init function:
self.canvas_height = self.canvas.winfo_height()
self.canvas_width = self.canvas.winfo_width()
to these lines:
self.canvas_height = self.canvas.winfo_reqheight()
self.canvas_width = self.canvas.winfo_reqwidth()
I am writing a simple program in which I want the (ball image png) to bounce off the walls. So far, I have writen this code:
import tkinter as tk
root = tk.Tk()
WIDTH = 500
HEIGHT = 500
canvas = tk.Canvas(root,bg="white",width=WIDTH,height=HEIGHT)
canvas.pack()
img = tk.PhotoImage(file="images/ball1.png")
ball = canvas.create_image(0,0,anchor="nw",image=img)
yspeed = 2
xspeed = 2
def move_ball():
global xspeed,yspeed,ball
canvas.move(ball,xspeed,yspeed)
canvas.after(10,move_ball)
move_ball()
root.mainloop()
You can get the current position with the coords method, then code in a check for each of the 4 walls. Here's the first one:
def move_ball():
global xspeed,yspeed,ball
xpos, ypos = canvas.coords(ball)
if xpos + width_of_ball > WIDTH:
# ball hit the right edge, reverse x direction
xspeed *= -1
canvas.move(ball,xspeed,yspeed)
canvas.after(10,move_ball)
This answer is the same thing as #Novel answer (even though I wrote it before I saw theirs). The only difference is in the fact that the logic of update doesn't expect you to make any edits for it to work, it considers both horizontal and vertical directions, and resizing the main window is compensated for.
import tkinter as tk
root = tk.Tk()
root.title('Infinite Bounce Simulator')
root.geometry('400x300+300+300')
xspeed = 4
yspeed = 3
canvas = tk.Canvas(root, highlightthickness=0, bg='#111')
canvas.pack(expand=True, fill='both')
ball = canvas.create_oval((0, 0, 20, 20), fill='red')
def update():
global xspeed, yspeed, ball
canvas.move(ball, xspeed, yspeed)
#Left, Top, Right, Bottom coordinates
l, t, r, b = canvas.coords(ball)
#flip speeds when edges are reached
if r > canvas.winfo_width() or l < 0:
xspeed = -xspeed
if b > canvas.winfo_height() or t < 0:
yspeed = -yspeed
#do it all again in 10 milliseconds
root.after(10, update)
root.after_idle(update)
root.mainloop()
Started playing with python's tkinter today and ran into some problems.
I created an animation that moves a ball around the screen, with a given speed. (and when it hits the screen, it goes back)
Why does my ball look bad? it's shape is not uniform? (its like blinking a lot)
Is there a better way to do it?
the code:
from tkinter import *
import time
WIDTH = 800
HEIGHT = 500
SIZE = 100
tk = Tk()
canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="grey")
canvas.pack()
color = 'black'
class Ball:
def __init__(self):
self.shape = canvas.create_oval(0, 0, SIZE, SIZE, fill=color)
self.speedx = 3
self.speedy = 3
def update(self):
canvas.move(self.shape, self.speedx, self.speedy)
pos = canvas.coords(self.shape)
if pos[2] >= WIDTH or pos[0] <= 0:
self.speedx *= -1
if pos[3] >= HEIGHT or pos[1] <= 0:
self.speedy *= -1
ball = Ball()
while True:
ball.update()
tk.update()
time.sleep(0.01)
errors after terminating the program:
Traceback (most recent call last):
File "C:/..py", line 29, in <module>
ball.update()
File "C:/Users/talsh/...py", line 20, in update
canvas.move(self.shape, self.speedx, self.speedy)
File "C:\Users\...\tkinter\__init__.py", line 2585, in move
self.tk.call((self._w, 'move') + args)
_tkinter.TclError: invalid command name ".!canvas"
Is it normal? Am I doing anything wrong?
I would imaging the problem is coming from sleep(). The methods sleep() and wait() should not be used in tkinter as they will pause the entire application instead of just providing a timer.
Update:
Its also not a good idea to name a method the same name as a built in method.
you have self.update() and update() is already in the name space for canvas. Change self.update() to something else like: self.ball_update()
UPDATE:
It looks like tikinter refreshes at a 15ms rate and trying to fire an even faster than that might cause issues. The closest I was able to get to stopping the circle from distorting while moving at the same rate as your original code was to change the timer to 30ms and to change your speed variables to 9 from 3.
Always make sure you have mainloop() at the end of you tkinter app. mainloop() is required to make sure tkinter runs properly and without there may be bugs caused by it missing so at the end add tk.mainloop()
You should use after() instead. This should probably be done using a function/method as your timed loop. Something like this:
def move_active(self):
if self.active == True:
self.ball_update()
tk.after(30, self.move_active)
tk.update()
Replace your while loop with the above method and add the class attribute self.active = True to your __init__ section. Let me know if this clears up your stuttering:
from tkinter import *
import time
WIDTH = 800
HEIGHT = 500
SIZE = 100
tk = Tk()
canvas = Canvas(tk, width=WIDTH, height=HEIGHT, bg="grey")
canvas.pack()
color = 'black'
class Ball:
def __init__(self):
self.shape = canvas.create_oval(0, 0, SIZE, SIZE, fill=color)
self.speedx = 9 # changed from 3 to 9
self.speedy = 9 # changed from 3 to 9
self.active = True
self.move_active()
def ball_update(self):
canvas.move(self.shape, self.speedx, self.speedy)
pos = canvas.coords(self.shape)
if pos[2] >= WIDTH or pos[0] <= 0:
self.speedx *= -1
if pos[3] >= HEIGHT or pos[1] <= 0:
self.speedy *= -1
def move_active(self):
if self.active == True:
self.ball_update()
tk.after(30, self.move_active) # changed from 10ms to 30ms
ball = Ball()
tk.mainloop() # there should always be a mainloop statement in tkinter apps.
Here are some links to Q/A's related to refresh timers.
Why are .NET timers limited to 15 ms resolution?
Why does this shape in Tkinter update slowly?
All that being said you may want to use an alternative that might be able to operate at a faster refreash rate like Pygame
UPDATE:
Here is an image of what is happening to the circle while its moving through the canvas. As you can see its getting potions of the circle visibly cut off. This appears to happen the faster the update is set. The slower the update( mostly above 15ms) seams to reduce this problem:
After suffering the same flattened leading edges of fast moving objects I am inclined to agree with #Fheuef's response, although I solved the problem in a different manner.
I was able to eliminate this effect by forcing a redraw of the entire canvas just by resetting the background on every update.
Try adding:
canvas.configure(bg="grey")
to your loop. Of course we compromise performance, but it's a simple change and it seems a reasonable trade off here.
Basically I've found that this has to do with the way Tkinter updates the canvas image : instead of redrawing the whole canvas everytime, it forms a box around things that have moved and it redraws that box. The thing is, it seems to use the ball's old position (before it moved) so if the ball moves too fast, its new position is out of the redraw box.
One simple way to solve this however is to create a larger invisible ball with outline='' around it, which will move to the ball's position on every update, so that the redraw box takes that ball into account and the smaller one stays inside of it. Hope that's clear enough...
I am having a little trouble with this project. I have to create a pendulum using key handles and the code I have for the key's up and down don't seem to be working. "up" is suppose to make the pendulum go faster and "down" makes it go slower. This is the code that I have so far. can somebody please help.
from tkinter import * # Import tkinter
import math
width = 200
height = 200
pendulumRadius = 150
ballRadius = 10
leftAngle = 120
rightAngle = 60
class MainGUI:
def __init__(self):
self.window = Tk() # Create a window, we may call it root, parent, etc
self.window.title("Pendulum") # Set a title
self.canvas = Canvas(self.window, bg = "white",
width = width, height = height)
self.canvas.pack()
self.angle = leftAngle # Start from leftAngle
self.angleDelta = -1 # Swing interval
self.delay = 200
self.window.bind("<Key>",self.key)
self.displayPendulum()
self.done = False
while not self.done:
self.canvas.delete("pendulum") # we used delete(ALL) in previous lab
# here we only delete pendulum object
# in displayPendulum we give the tag
# to the ovals and line (pendulum)
self.displayPendulum() # redraw
self.canvas.after(self.delay) # Sleep for 100 milliseconds
self.canvas.update() # Update canvas
self.window.mainloop() # Create an event loop
def displayPendulum(self):
x1 = width // 2;
y1 = 20;
if self.angle < rightAngle:
self.angleDelta = 1 # Swing to the left
elif self.angle > leftAngle:
self.angleDelta = -1 # Swing to the right
self.angle += self.angleDelta
x = x1 + pendulumRadius * math.cos(math.radians(self.angle))
y = y1 + pendulumRadius * math.sin(math.radians(self.angle))
self.canvas.create_line(x1, y1, x, y, fill="blue", tags = "pendulum")
self.canvas.create_oval(x1 - 2, y1 - 2, x1 + 2, y1 + 2,
fill = "red", tags = "pendulum")
self.canvas.create_oval(x - ballRadius, y - ballRadius,
x + ballRadius, y + ballRadius,
fill = "green", tags = "pendulum")
def key(self,event):
print(event.keysym)
print(self.delay)
if event.keysym == 'up':
print("up arrow key pressed, delay is",self.delay)
if self.delay >10:
self.delay -= 1
if event.keysym == 'Down':
print("Down arrow key pressed,delay is",self.delay)
if self.delay < 200:
self.delay += 1
if event.keysym=='q':
print ("press q")
self.done = True
self.window.destroy()
MainGUI()
The root of the problem is that the event keysym is "Up" but you are comparing it to the all-lowercase "up". Naturally, the comparison fails every time. Change the if statement to if event.keysym == 'Up':
Improving the animation
In case you're interested, there is a better way to do animation in tkinter than to write your own infinite loop. Tkinter already has an infinite loop running (mainloop), so you can take advantage of that.
Create a function that draws one frame, then have that function arrange for itself to be called again at some point in the future. Specifically, remove your entire "while" loop with a single call to displayFrame(), and then define displayFrame like this:
def drawFrame(self):
if not self.done:
self.canvas.delete("pendulum")
self.displayPendulum()
self.canvas.after(self.delay, self.drawFrame)
Improving the bindings
Generally speaking, your code will be marginally easier to manage and test if you have specific bindings for specific keys. So instead of a single catch-all function, you can have specific functions for each key.
Since your app supports the up key, the down key, and the "q" key, I recommend three bindings:
self.window.bind('<Up>', self.onUp)
self.window.bind('<Down>', self.onDown)
self.window.bind('<q>', self.quit)
You can then define each function to do exactly one thing:
def onUp(self, event):
if self.delay > 10:
self.delay -= 1
def onDown(self, event):
if self.delay < 200:
self.delay += 1
def onQuit(self, event):
self.done = True
self.window.destroy()
Both items will move fine however, whenever I am using the bound keys to the event handler the other image (enemy) will disappear until i stop moving the player with the keys. Is there a way around this? I've tried the canvas method .move also and tried searching didn't find a solution from the searches I tried.
def move_player(self, event):
if event.keysym.lower() == "a":
self.player_x -= self.player_delta_x
if event.keysym.lower() == "d":
self.player_x += self.player_delta_x
self.canvas.delete(self.player)
self.make_player()
def move_enemy(self):
self.enemy_x += self.enemy_delta_x
self.enemy_y -= self.enemy_delta_y
#Keep the enemy within the bounds of the screen
if self.enemy_x > self.width - self.enemy_radius:
self.enemy_delta_x *= -1
if self.enemy_x < 0:
self.enemy_delta_x *= -1
if (self.enemy_y >= height / 2 or
self.enemy_y <= 0 + self.enemy_radius):
self.enemy_delta_y *= -1
self.canvas.delete(self.enemy)
self.make_enemy()
if __name__ == "__main__":
root = tk.Tk()
canvas = tk.Canvas(root, bg = "black")
width = root.winfo_screenwidth()
height = root.winfo_screenheight() - TASKBAR_OFFSET
root.geometry("%dx%d" % (width, height))
game = Game(root, canvas, width, height)
game.make_menu()
game.make_background()
game.make_player()
game.make_enemy()
while True:
game.move_enemy()
game.canvas.update()
time.sleep(0.025)
root.mainloop()
First of all, instead of using a while True loop, use root.after(0, animateFunction).
And then inside of the animate function:
def animateFunction():
#Movement Code
moveEnemyFunction()
checkPlayerMovement()
root.after(animateFunction, 10)
If you loop like this, it should complete the movement task each frame and ultimately move the player without losing the enemy each frame the player moves.
Now, there is no guarantee this will work, because I have no idea how your movement works and how you bound it, but I believe this should solve your problem.