LoginSignup
47
14

More than 5 years have passed since last update.

【Python】「rm -rf /」ドッキリ

Posted at

作ったもの

rm.gif
冒頭のプログラム実行以降は,全て自動で行っています。

プログラム

ソースコード全体

マウス操作

本物のターミナルを開いている感を出すために,dockのアイコンまで移動します。もちろん本当にクリックはしません。その後,開いた疑似ターミナル画面まで動きます。

import pyautogui as pgui
from time import sleep

def cursor_animation():
    pgui.moveTo(1500, 480, duration=1)  # dock
    pgui.moveTo(1420, 480, duration=1)  # ターミナルアイコン
    sleep(0.6)
    pgui.moveTo(100, 300, duration=1)   # ターミナル画面

疑似ターミナル

できるだけ自分のターミナルに似せました。

import tkinter as tk

class Terminal(tk.Tk):
    def __init__(self):
        super().__init__()

        self.title('ターミナル ー -bash ー 80×24')
        self.configure(width=750, height=450)

        self.wtext = tk.Text(self,
                             width=50, height=40,
                             padx=10, pady=8,
                             bg='#313131', fg='#ffffff',
                             wrap=tk.CHAR,
                             font=('Osaka', 14))
        self.wtext.place(x=-5, y=-5, width=760, height=460)
        self.wtext.tag_config('prompt', foreground='#00FFFF')
        self.wtext.tag_config('warning', foreground='#E52121')
        self.wtext.tag_config('error', foreground='#E5E500')

コマンドアニメーション

(次に追加する文字列,追加後に待つ時間[msec],タグ)を返すジェネレータを作成し,それを元にafter()関数で再帰的に追加タイマーを作成します。

def frame_gen(self):
    ''' animation frames '''
    # (text, wait_time, tag)
    frames = (
        ('Last login: Thu Jan 10 12:45:36 on ttys000\n', 1000, ''),
        (f'{os.uname()[1].split(".")[0]}:~ {os.environ["USER"]}$ ', 2000, 'prompt')
    )
    for f in frames:
        yield f

def animation(self):
    ''' タイマー設定 '''
    try:
        next_frame, wait, tag = next(self.frame)
    except:
        return
    last_insert = self.wtext.index(tk.INSERT)
    self.wtext.insert(tk.INSERT, next_frame)
    self.wtext.tag_add(tag, last_insert, tk.INSERT)
    self.wtext.see('end')
    self.after(wait, self.animation)

備考

使用する際は各パラメータを自分用にカスタマイズする必要があります。

47
14
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
47
14