Deutsch English Français Italiano |
<windows-20240908202958@ram.dialup.fu-berlin.de> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!news.mixmin.net!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: ram@zedat.fu-berlin.de (Stefan Ram) Newsgroups: comp.lang.python Subject: POTD: Linux-Windows TUI Date: 8 Sep 2024 19:41:39 GMT Organization: Stefan Ram Lines: 73 Expires: 1 Jul 2025 11:59:58 GMT Message-ID: <windows-20240908202958@ram.dialup.fu-berlin.de> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Trace: news.uni-berlin.de BfHvEYzwFdBCXDz9Wuc6vQmTcq5ktzkiY5Dw+pSvzRIsLX Cancel-Lock: sha1:2X0UqCV7N2GbFNhJ8JrGEw7rDzs= sha256:vgUoiKerQyGHi6Ik4JNyIlXX3BnXwHI5NaEkCCRtWt4= X-Copyright: (C) Copyright 2024 Stefan Ram. All rights reserved. Distribution through any means other than regular usenet channels is forbidden. It is forbidden to publish this article in the Web, to change URIs of this article into links, and to transfer the body without this notice, but quotations of parts in other Usenet posts are allowed. X-No-Archive: Yes Archive: no X-No-Archive-Readme: "X-No-Archive" is set, because this prevents some services to mirror the article in the web. But the article may be kept on a Usenet archive server with only NNTP access. X-No-Html: yes Content-Language: en-US Bytes: 3317 Whipped up this gnarly POTD ("program of the day") in a hot minute. My AI homie threw me a bone. It wipes the slate clean and spits out a rando number when you smash the space bar. This bad boy only needs the standard library. It's golden on Linux and Windows. On Linux, where there's curses in the mix, we roll with that. On Windows, we kick it with tkinter instead. Use this as your secret sauce for portable TUI software! If there are still any bugs, that's on me. Don't throw shade at my AI wingman! import random import sys import os # Determine the operating system is_windows = os.name == 'nt' if is_windows: import tkinter as tk else: import curses class RandomNumberGenerator: def __init__(self): self.random_range = (1, 1000) def generate_number(self): return random.randint(*self.random_range) class LinuxApp(RandomNumberGenerator): def run(self): def main(stdscr): while True: key = stdscr.getch() if key == ord(' '): stdscr.clear() stdscr.addstr(str(self.generate_number())) stdscr.refresh() elif key == ord('q'): break curses.wrapper(main) class WindowsApp(RandomNumberGenerator): def run(self): root = tk.Tk() root.title("Random Number Generator") text_widget = tk.Text(root, height=5, width=30) text_widget.pack(padx=10, pady=10) text_widget.insert(tk.END, "Press SPACE to generate a random number") def generate_number_gui(event): text_widget.delete(1.0, tk.END) text_widget.insert(tk.END, str(self.generate_number())) root.bind('<space>', generate_number_gui) root.mainloop() def main(): app = WindowsApp() if is_windows else LinuxApp() app.run() if __name__ == "__main__": main()