I have programmed a virus prank in Python with the tkinter module. The program creates a window with a picture of Jake Paul's face on it. The window bounces around the screen.

I am planning on compiling to code into an exe file. I would like advice on how to improve the performance of the program rather than readability.


import tkinter
from random import randint
from pygame import mixer
from time import sleep

# Create a window:
window = tkinter.Tk()

# Setup the image:
img_obj = tkinter.PhotoImage(file='jake.png')
img_label = tkinter.Label(window, image=img_obj)
img_label.image = 'jake.png'

width, height = img_obj.width(), img_obj.height()

# Setup the windows:
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()

window.resizable(False, False)

def random_x():
    return randint(0, screen_width - width)

def random_y():
    return randint(0, screen_height - height)

def random_vel():
    speed = 10

    vel = randint(-speed, speed)

    if vel == 0:
        return random_vel()

    return vel

x = random_x()
y = random_y()
x_vel = random_vel()
y_vel = random_vel()

# Setup the sound:
sound = 'walmart30mins.mp3'

# Main loop:
while True:


    if x >= (screen_width - width) and x_vel > 0:
        x_vel = -x_vel

    elif x <= 0 and x_vel < 0:
        x_vel = -x_vel

    if y >= (screen_height - height) and y_vel > 0:
        y_vel = -y_vel
    elif y <= 0 and y_vel < 0:
        y_vel = -y_vel

    x += x_vel
    y += y_vel

    if randint(1, 100) == 100:
        x_vel = random_vel()
        y_vel = random_vel()

    window_string = str(width) + 'x' + str(height) + '+' + str(x) + '+' + str(y)


  • \$\begingroup\$ Well my 1st bit of advice is to not use a while loop or sleep in conjunction with tkinter. Instead you can manage your loop within the mainloop and use after() to manage calculations. \$\endgroup\$ – Sierra Mountain Tech Dec 18 '18 at 14:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.