-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBatMenu.py
More file actions
204 lines (163 loc) · 5.66 KB
/
BatMenu.py
File metadata and controls
204 lines (163 loc) · 5.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/env python3
"""
Program : BatMenu
Author : Jon Freivald <jfreivald@brmedical.com>
: Copyright © Blue Ridge Medical Center, 2025. All Rights Reserved.
: License: Creative Commons
Date : 2025-09-29
Purpose : GUI Menu wrapper for batch files
: Version change log at EoF.
"""
import os
import sys
from PySide6.QtGui import (
QFont,
)
from PySide6 import (
QtCore,
)
from PySide6.QtCore import (
QPoint,
QProcess,
QSettings,
QSize,
)
from PySide6.QtWidgets import (
QApplication,
QGridLayout,
QMainWindow,
QPushButton,
QTextEdit,
QVBoxLayout,
QWidget,
)
progver = '1.01'
brmc_dark_blue = '#00446a'
brmc_medium_blue = '#73afb6'
brmc_gold = '#ffcf01'
brmc_rust = '#ce7067'
brmc_warm_grey = '#9a8b7d'
light_grey = '#d3d3d3'
flat_white = '#e7e7e7'
num_cols = 5
buttons = []
# The following 2 lines are required in case we are running with pythonw.exe
if sys.stdout is None: sys.stdout = open(os.devnull, "w")
if sys.stderr is None: sys.stderr = open(os.devnull, "w")
def listbats():
files = []
for f in os.listdir():
if f.endswith(".bat"):
files.append(f)
return files
def win_title():
try:
with open("batmenu.cfg", 'r') as fp:
win_title = fp.readline()
except:
win_title = "Batch File Menu System"
return win_title
win_title = win_title()
class Scripts:
def __init__(self, script):
self.name = script.strip('.bat')
self.script = script
self.button = QPushButton(self.name)
self.button.setStyleSheet(f'background-color: {brmc_dark_blue}; color: {brmc_gold}')
self.button.clicked.connect(self.display)
def get_button(self):
return self.button
def display(self):
self.out = DataWindow(self.name, self.script)
self.out.show()
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
self.settings = QSettings("Blue Ridge MEdical Center", win_title)
self.resize(self.settings.value('MainWIndowSize', QSize(180, 30)))
self.move(self.settings.value('MainWindowPos', QPoint(50, 50)))
self.setStyleSheet(f'background-color: {brmc_medium_blue}')
self.setWindowTitle(f"{win_title}, v {progver}")
container = QWidget()
layout = QGridLayout()
for file in listbats():
buttons.append(Scripts(file))
x = 0
y = 0
for b in buttons:
layout.addWidget(b.get_button(), y, x)
x += 1
if x >= num_cols:
x = 0
y += 1
container.setLayout(layout)
self.setCentralWidget(container)
def closeEvent(self, a0):
self.settings.setValue('MainWindowSize', self.size())
self.settings.setValue('MainWindowPos', self.pos())
return super().closeEvent(a0)
class DataWindow(QWidget):
def __init__(self, name, file):
super().__init__()
self.name = name
self.file = file
self.settings = QSettings("Blue Ridge MEdical Center", win_title)
self.resize(self.settings.value(f'{self.name}WIndowSize', QSize(850, 400)))
self.move(self.settings.value(f'{self.name}WindowPos', QPoint(50, 50)))
self.setContentsMargins(10, 10, 10, 10)
self.setStyleSheet(f'background-color: {brmc_medium_blue}')
self.p = None
self.current_process = None
self.text = QTextEdit()
font = QFont("Cascadia Code", 10)
self.text.setFont(font)
self.text.setStyleSheet(f'background-color: {flat_white}; color: black')
self.cursor = self.text.textCursor()
self.cursor.setPosition(0)
self.text.setTextCursor(self.cursor)
self.text.setReadOnly(True)
layout = QVBoxLayout()
layout.addWidget(self.text)
self.setLayout(layout)
self.current_process = name
self.start_process(name, file)
def message(self, s):
self.text.insertPlainText(s)
self.text.setTextCursor(self.cursor)
def start_process(self, which, file):
self.which = which
if self.p is None: # No process running.
self.p = QProcess() # Keep a reference to the QProcess (e.g. on self) while it's running.
self.p.readyReadStandardOutput.connect(self.handle_stdout)
self.p.readyReadStandardError.connect(self.handle_stderr)
self.p.stateChanged.connect(self.handle_state)
self.p.finished.connect(self.process_finished) # Clean up once complete.
self.p.start("cmd", ['/c', file])
def handle_stderr(self):
data = self.p.readAllStandardError()
stderr = bytes(data).decode(sys.stdout.encoding)
self.message(stderr)
def handle_stdout(self):
data = self.p.readAllStandardOutput()
stdout = bytes(data).decode(sys.stdout.encoding)
self.message(stdout)
def handle_state(self, state):
if QProcess.Running:
self.setWindowTitle(f"{self.current_process} running")
def process_finished(self):
self.setWindowTitle(f"{self.current_process} finished")
self.p = None
def closeEvent(self, a0):
self.settings.setValue(f'{self.name}WindowSize', self.size())
self.settings.setValue(f'{self.name}WindowPos', self.pos())
return super().closeEvent(a0)
if __name__ == '__main__':
app = QApplication(sys.argv)
window = MainWindow()
window.show()
sys.exit(app.exec())
"""
change log:
v 1.0 : 250929 : Started with MyBack v 2.0 and modified to be a generic batch file menu system.
v 1.01 : 251030 : QSettings are now separate for each instance running with a config file
"""