1
\$\begingroup\$

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.

JAKE.PY:

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'
img_label.pack()

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

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


window.title('JAKE.EXE')
window.resizable(False, False)
window.overrideredirect(True)


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:
mixer.init()
sound = 'walmart30mins.mp3'
mixer.music.load(sound)
mixer.music.play()

# Main loop:
while True:

    sleep(0.01)

    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)

    window.geometry(window_string)

    window.update()
\$\endgroup\$
  • \$\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.