Skip to content

Instantly share code, notes, and snippets.

@roflsunriz
Last active March 31, 2025 11:28
Show Gist options
  • Select an option

  • Save roflsunriz/1a7ae02ae9dd922a10701f46692c5fa1 to your computer and use it in GitHub Desktop.

Select an option

Save roflsunriz/1a7ae02ae9dd922a10701f46692c5fa1 to your computer and use it in GitHub Desktop.
NexusLauncher_PyQt : Start, Forceibly Close, Restart, Auto Close for Winstep Nexus
import subprocess
import sys
import ctypes
# 必要なライブラリのリスト
REQUIRED_LIBRARIES = [
'PyQt5',
'ctypes',
'pywinstyles'
]
def install_libraries():
for lib in REQUIRED_LIBRARIES:
try:
__import__(lib)
print(f"{lib} は既にインストールされています")
except ImportError:
print(f"{lib} をインストール中...")
subprocess.check_call([sys.executable, "-m", "pip", "install", lib])
# ライブラリのインストールを確認
install_libraries()
# PyQt5とpywinstylesをインポート
from PyQt5 import QtWidgets, QtCore, QtGui
import pywinstyles
class NexusLauncher(QtWidgets.QWidget):
def __init__(self):
super().__init__()
self.initUI()
def initUI(self):
# ウィンドウ設定
self.setWindowTitle('Nexus Launcher')
self.setGeometry(300, 300, 300, 250)
# pywinstylesを使用してウィンドウスタイルを設定
pywinstyles.apply_style(self, 'transparent')
# タイトルバーにアクセントカラーを設定
accent_color = pywinstyles.get_accent_color()
pywinstyles.change_header_color(self, color=accent_color)
pywinstyles.change_title_color(self, color="white")
# メインウィジェットを作成
self.main_widget = QtWidgets.QWidget(self)
# レイアウト
layout = QtWidgets.QVBoxLayout(self.main_widget)
layout.setContentsMargins(10, 10, 10, 10)
# ボタン作成
btn_launch = QtWidgets.QPushButton('Nexusを起動')
btn_kill = QtWidgets.QPushButton('Nexusを終了')
btn_restart = QtWidgets.QPushButton('Nexusを再起動')
btn_auto_kill = QtWidgets.QPushButton('応答無しのプロセスを終了')
# ボタンにスタイルを適用
button_style = """
QPushButton {
background-color: rgba(255, 255, 255, 30);
color: white;
border: 1px solid rgba(255, 255, 255, 40);
border-radius: 5px;
padding: 10px;
font-weight: bold;
}
QPushButton:hover {
background-color: rgba(255, 255, 255, 50);
border: 1px solid rgba(255, 255, 255, 60);
}
QPushButton:pressed {
background-color: rgba(255, 255, 255, 20);
}
"""
for btn in [btn_launch, btn_kill, btn_restart, btn_auto_kill]:
btn.setStyleSheet(button_style)
# ボタン配置
layout.addWidget(btn_launch)
layout.addWidget(btn_kill)
layout.addWidget(btn_restart)
layout.addWidget(btn_auto_kill)
# ステータスバー
self.status_bar = QtWidgets.QLabel('準備完了')
self.status_bar.setStyleSheet("""
QLabel {
color: white;
padding: 5px;
background-color: rgba(255, 255, 255, 10);
border-radius: 3px;
}
""")
layout.addWidget(self.status_bar)
self.setLayout(layout)
# ボタン接続
btn_launch.clicked.connect(self.launch)
btn_kill.clicked.connect(self.kill)
btn_restart.clicked.connect(self.restart)
btn_auto_kill.clicked.connect(self.auto_kill)
def update_status(self, message):
self.status_bar.setText(message)
def launch(self):
try:
subprocess.Popen([r"C:\Program Files (x86)\Winstep\Nexus.exe"], creationflags=subprocess.CREATE_NO_WINDOW)
self.update_status("Nexusが正常に起動しました")
except Exception as e:
self.update_status(f"起動に失敗: {str(e)}")
def kill(self):
try:
subprocess.run(["taskkill", "/f", "/im", "nexus.exe"], creationflags=subprocess.CREATE_NO_WINDOW)
self.update_status("Nexusが正常に終了しました")
except Exception as e:
self.update_status(f"終了に失敗: {str(e)}")
def restart(self):
try:
subprocess.run(["taskkill", "/f", "/im", "nexus.exe"], creationflags=subprocess.CREATE_NO_WINDOW)
subprocess.Popen([r"C:\Program Files (x86)\Winstep\Nexus.exe"], creationflags=subprocess.CREATE_NO_WINDOW)
self.update_status("Nexusが正常に再起動しました")
except Exception as e:
self.update_status(f"再起動に失敗: {str(e)}")
def auto_kill(self):
try:
subprocess.run(["taskkill.exe", "/f", "/fi", "status eq not responding"], creationflags=subprocess.CREATE_NO_WINDOW)
self.update_status("応答なしのプロセスを終了しました")
except Exception as e:
self.update_status(f"自動終了に失敗: {str(e)}")
def resizeEvent(self, event):
# メインウィジェットのサイズを調整
self.main_widget.resize(self.size())
super().resizeEvent(event)
def mousePressEvent(self, event):
if event.button() == QtCore.Qt.LeftButton:
self.drag_pos = event.globalPos()
def mouseMoveEvent(self, event):
if event.buttons() == QtCore.Qt.LeftButton:
self.move(self.pos() + event.globalPos() - self.drag_pos)
self.drag_pos = event.globalPos()
event.accept()
if __name__ == '__main__':
app = QtWidgets.QApplication(sys.argv)
launcher = NexusLauncher()
launcher.show()
sys.exit(app.exec_())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment