Open10

適当にPythonをいじる

坦々狸坦々狸

スプラッシュしてみる

# -*- coding: utf8 -*-
from tkinter import *

#タイトルバー非表示で画面生成
splash = Tk()
splash.overrideredirect(1) 

#モニターの縦横幅取得
sw=splash.winfo_screenwidth() 
sh=splash.winfo_screenheight() #モニターの縦幅取得

# 画像ロード
photo = PhotoImage(file="splash.png")

#画像の縦横幅取得
pw=photo.width()
ph=photo.height()

# 描画
canvas = Canvas(width=pw, height=ph)
canvas.place(x=0, y=0)
canvas.create_image(0, 0, image=photo, anchor=NW)

#モニターの中央に画面を表示
splash.geometry(str(pw)+"x"+str(ph)+"+"+str((sw-pw)//2)+"+"+str((sh-ph)//2)) 

# 画面非表示
# splash.withdraw();

splash.after(1000,splash.destroy)

mainloop()
坦々狸坦々狸

スプラッシュ中に処理実行


# -*- coding: utf8 -*-
from tkinter import *

class ShowSplash:

    def start(self):
        self.running = True

        # タイトルバー非表示で画面生成
        self.splash = Tk()
        self.splash.overrideredirect(1)

        # モニターの縦横幅取得
        sw = self.splash.winfo_screenwidth()
        sh = self.splash.winfo_screenheight()

        # 画像ロード
        photo = PhotoImage(file="splash.png")

        # 画像の縦横幅取得
        pw = photo.width()
        ph = photo.height()

        # 描画
        canvas = Canvas(width=pw, height=ph)
        canvas.place(x=0, y=0)
        canvas.create_image(0, 0, image=photo, anchor=NW)
        canvas.create_text(pw/2, ph-24, text="Loading...", font=("",24))

        # モニターの中央に画面を表示
        self.splash.geometry(str(pw)+"x"+str(ph)+"+" +
                             str((sw-pw)//2)+"+"+str((sh-ph)//2))

        self.splash.after(100, self._check_to_quit)
        self.splash.mainloop()

        # need to delete variables that reference tkinter objects in the thread
        del self.splash

    def _check_to_quit(self):
        if self.running:
            self.splash.after(100, self._check_to_quit)
        else:
            self.splash.destroy()

    def quit(self):
        self.running = False
# スプラッシュ画像表示
splash = ShowSplash()
thread = threading.Thread(target=splash.start)
thread.start()

# 何か処理


# スプラッシュ画像を閉じる
splash.quit()
thread.join()
坦々狸坦々狸

venv

$ python3 -m venv test
$ cd test
$ source bin/activate
(test) $ which python
/tmp/test/bin/python
(test) $ which pip
/tmp/test/bin/pip
坦々狸坦々狸

VPNGateからサーバ一覧を取得

# -*- coding: utf8 -*-
from urllib import request
from io import StringIO
import base64
import csv

# VPNGateからサーバ一覧を取得
def getServerList() -> list:
    url = 'http://www.vpngate.net/api/iphone/'
    req = request.Request(url)
    with request.urlopen(req) as res:
        # BODYをCSVとして読み込み
        with StringIO() as f:
            f.write(res.read().decode())
            f.seek(0)
            csv_reader = csv.reader(f)

            # ヘッダ行の読み飛ばし
            next(csv_reader)
            next(csv_reader)

            # サーバリストの生成
            rows = []
            for row in csv_reader:
                # フッタ行以外を読み込み
                if ( (row[0] != '*') & (len(row) == 15) ):
                    # base64エンコーディングされたカラムをデコード
                    row[14] = base64.b64decode(row[14]).decode()
                    rows.append(row)

            # 生成したサーバリストを返す
            return rows
坦々狸坦々狸

pip install してもパス通らないから.bashrcに追記

export PATH=$HOME/.local/bin:$PATH
坦々狸坦々狸

pyenvでバージョン切り替え(dockerのUbuntu20.04コンテナで確認)

依存関係のインストール

https://github.com/pyenv/pyenv/wiki#suggested-build-environment

# apt-get update
# apt-get install -y git make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev libffi-dev liblzma-dev

本体のインストール
https://github.com/pyenv/pyenv#basic-github-checkout

$ git clone https://github.com/pyenv/pyenv.git ~/.pyenv
$ cd ~/.pyenv && src/configure && make -C src

環境変数へとりこみ

# sed -Ei -e '/^([^#]|$)/ {a \
export PYENV_ROOT="$HOME/.pyenv"
a \
export PATH="$PYENV_ROOT/bin:$PATH"
a \
' -e ':a' -e '$!{n;ba};}' ~/.profile
# echo 'eval "$(pyenv init --path)"' >>~/.profile
# echo 'eval "$(pyenv init -)"' >> ~/.bashrc

使ってみる

# pyenv install 2.7.13
Downloading Python-2.7.13.tar.xz...
-> https://www.python.org/ftp/python/2.7.13/Python-2.7.13.tar.xz
Installing Python-2.7.13...
Installed Python-2.7.13 to /root/.pyenv/versions/2.7.13

# pyenv global 2.7.13
# which python
/root/.pyenv/shims/python
# python --version
Python 2.7.13
坦々狸坦々狸

poetryを使うならvenvはpoetry installで作ってくれる

# pip install --upgrade pip
# pip install poetry
# poetry new python-my-project
# cd python-my-project
# poetry config virtualenvs.in-project true
# poetry install
# poetry shell
(.venv) root@660c6d879c15:~/pyenv-3.9.7/my-project#

newしたディレクトリ配下でpyenvするとpyproject.tomlのバージョンと変わるかも知れないので
pyenvでバージョン指定したディレクトリ配下でnewしたら良いと思う

坦々狸坦々狸

EOL迎えたような古いVersionのPythonを使わざるを得ない時はpythonの公式コンテナを使ってそれ以外は
pyenv入れたコンテナを用意しといて適宜VSCodeからリモートコンテナで入って作業するのが一番環境汚さずに済みそう