Created
December 24, 2024 12:44
-
-
Save refset/28e385fd800d6fe4f92c0a34454598a1 to your computer and use it in GitHub Desktop.
prompt.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import os | |
import subprocess | |
import sys | |
import tkinter as tk | |
class AnimatedTextApp: | |
def __init__(self, root, title="Animated Text"): | |
self.root = root | |
self.root.title(title) | |
# Remove window frame decorations | |
#self.root.overrideredirect(True) | |
# Set the window to always be on top | |
self.root.attributes("-topmost", True) | |
# Configure the window size and properties | |
self.root.geometry("800x200") | |
#self.root.resizable(False, False) | |
# Create a canvas to display the text | |
self.canvas = tk.Canvas(self.root, width=8000, height=2000, bg="black") | |
self.canvas.pack() | |
# Initialize text properties | |
self.text_id = self.canvas.create_text(80, 80, text="", fill="#ffb000", font=("monospace", 50), anchor="w") | |
self.data = "" | |
self.offset = 0 | |
self.speed = 5 # Number of pixels to move per frame | |
self.animation_speed = 16 # Milliseconds between updates (8x slower than default) | |
# Bind key events for controls | |
self.root.bind("<Key-q>", self.close_window) # Quit | |
self.root.bind("<Key-equal>", self.increase_speed) # Increase speed | |
self.root.bind("<Key-minus>", self.decrease_speed) # Decrease speed | |
def set_data(self, text): | |
self.data = text | |
def close_window(self, event=None): | |
"""Gracefully close the window.""" | |
print("Exiting animation.") | |
self.root.destroy() | |
def increase_speed(self, event=None): | |
"""Increase the animation speed.""" | |
self.animation_speed = max(10, self.animation_speed - 20) # Reduce interval, minimum 10ms | |
print(f"Speed increased: {self.animation_speed}ms/frame") | |
def decrease_speed(self, event=None): | |
"""Decrease the animation speed.""" | |
self.animation_speed = round(1.1 * self.animation_speed) # Increase interval | |
print(f"Speed decreased: {self.animation_speed}ms/frame") | |
def animate(self): | |
"""Handle the animation logic.""" | |
try: | |
# Calculate the width of the text and canvas | |
text_width = self.canvas.bbox(self.text_id)[2] - self.canvas.bbox(self.text_id)[0] | |
canvas_width = self.canvas.winfo_width() | |
# Move the text to the left by self.speed | |
self.offset -= self.speed | |
# Wrap around if the text is fully off the screen | |
if self.offset + text_width < 0: | |
self.offset = canvas_width | |
# Update the text position | |
self.canvas.coords(self.text_id, self.offset, 100) | |
# Schedule the next frame | |
self.root.after(self.animation_speed, self.animate) | |
except tk.TclError: | |
# Handle window closure | |
print("Exiting animation.") | |
def run(self): | |
"""Start the animation.""" | |
self.canvas.itemconfig(self.text_id, text=self.data) | |
self.animate() | |
self.root.mainloop() | |
def launch_persistent_animation(data, title="Animated Text"): | |
""" | |
Launch a detached background process for the animation. | |
""" | |
script_path = os.path.abspath(__file__) | |
command = ["python3", script_path, "--child", data, title] | |
subprocess.Popen(command, start_new_session=True) # Detached process | |
def run_animation_as_child(data, title): | |
""" | |
Run the animation in the current process as a child instance. | |
""" | |
root = tk.Tk() | |
app = AnimatedTextApp(root, title) | |
app.set_data(data) | |
try: | |
app.run() | |
except KeyboardInterrupt: | |
print("\nCtrl+C detected. Closing the animation.") | |
app.close_window() | |
def main(): | |
if "--child" in sys.argv: | |
# If launched as a child process, run the animation | |
_, _, data, title = sys.argv | |
run_animation_as_child(data, title) | |
else: | |
# Prompt user for input and launch a persistent animation process | |
user_text = input("Enter text to animate: ") | |
launch_persistent_animation(user_text) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment