-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathpyqt.py
205 lines (170 loc) · 7.4 KB
/
pyqt.py
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
205
import sys
from PyQt5 import QtCore, QtGui, QtWidgets
import argparse
import re
class MainWindow(QtWidgets.QMainWindow):
def __init__(self, parameters):
super(MainWindow, self).__init__()
self.groups = groups
self.radio_groups = []
self.sliderlabels = []
self.initUI()
def initUI(self):
self.pagelayout = QtWidgets.QVBoxLayout() #page layout
self.dbtnlayout = QtWidgets.QHBoxLayout() #layout for the default buttons
self.elmtlayout = QtWidgets.QVBoxLayout() #layout for the added widgets, stacks elements
#add layouts to the page
self.pagelayout.addLayout(self.dbtnlayout)
self.pagelayout.addLayout(self.elmtlayout)
#run, save, load, quit, help button
btn = QtWidgets.QPushButton(self)
btn.setText("run")
btn.clicked.connect(self.run)
self.dbtnlayout.addWidget(btn)
btn = QtWidgets.QPushButton(self)
btn.setText("save")
btn.clicked.connect(self.save)
self.dbtnlayout.addWidget(btn)
btn = QtWidgets.QPushButton(self)
btn.setText("load")
btn.clicked.connect(self.load)
self.dbtnlayout.addWidget(btn)
btn = QtWidgets.QPushButton(self)
btn.setText("quit")
btn.clicked.connect(self.quit)
self.dbtnlayout.addWidget(btn)
btn = QtWidgets.QPushButton(self)
btn.setText("help")
btn.clicked.connect(self.help)
self.dbtnlayout.addWidget(btn)
#set the main page layout
widget = QtWidgets.QWidget()
widget.setLayout(self.pagelayout)
scroll = QtWidgets.QScrollArea() #add scrollbar
scroll.setWidgetResizable(True)
scroll.setWidget(widget)
self.setGeometry(500, 100, 700, 500)
self.setCentralWidget(scroll)
self.createWidgetsFromGroups()
def run(self):
print('run')
def save(self):
print('save')
def load(self):
print('load')
def quit(self):
self.close()
print('quit')
def help(self):
print('help')
def createWidgetsFromGroups(self):
for group in self.groups:
group_type, group_name, options, default_option, help = group
if group_type == "RADIO":
print("radio button created")
new_group = QtWidgets.QButtonGroup()
self.radio_groups.append(new_group)
group_layout = QtWidgets.QHBoxLayout()
label = QtWidgets.QLabel(group_name+":")
group_layout.addWidget(label)
for option in options:
option = option.strip()
radio_button = QtWidgets.QRadioButton(option)
new_group.addButton(radio_button)
group_layout.addWidget(radio_button)
if option in default_option:
radio_button.setChecked(True)
self.elmtlayout.addLayout(group_layout)
elif group_type == "IFILE" or group_type == "OFILE" or group_type == "IDIR":
print("browse files button created")
group_layout = QtWidgets.QHBoxLayout()
label = QtWidgets.QLabel(group_name+":")
group_layout.addWidget(label)
btn = QtWidgets.QPushButton(self)
btn.setText("browse...")
def browse():
file = QtWidgets.QFileDialog.getOpenFileNames(self, "Select File", "")
print(file)
btn.clicked.connect(browse)
txt = QtWidgets.QLineEdit(self)
group_layout.addWidget(btn)
group_layout.addWidget(txt)
self.elmtlayout.addLayout(group_layout)
elif group_type == "CHECK":
print("checkbox created")
group_layout = QtWidgets.QHBoxLayout()
label = QtWidgets.QLabel(group_name+":")
group_layout.addWidget(label)
for option in options:
option = option.strip()
checkbox = QtWidgets.QCheckBox(option, self)
group_layout.addWidget(checkbox)
if option in default_option:
checkbox.setChecked(True)
self.elmtlayout.addLayout(group_layout)
elif group_type == "ENTRY":
print("textbox created")
group_layout = QtWidgets.QHBoxLayout()
label = QtWidgets.QLabel(group_name+":")
group_layout.addWidget(label)
txt = QtWidgets.QLineEdit(self)
txt.setText(default_option)
group_layout.addWidget(txt)
self.elmtlayout.addLayout(group_layout)
elif group_type == "SCALE":
group_layout = QtWidgets.QHBoxLayout()
label = QtWidgets.QLabel(group_name+":")
group_layout.addWidget(label)
options = ''.join(options)
options = options.split(':')
print("slider created")
#creates a horizontal slider
slider = QtWidgets.QSlider(self)
slider.setOrientation(QtCore.Qt.Horizontal)
slider.setSingleStep(int(float(options[2])*100))
slider.setPageStep(int(float(options[2])*100)) #moves the slider when clicking or up/down
slider.setRange(int(options[0])*100, int(options[1])*100)
slider.setValue(int(float(default_option[0])*100))
label_slider = QtWidgets.QLabel(str(default_option[0]))
slider.valueChanged.connect(lambda value, lbl=label_slider: self.updateLabel(lbl, value))
group_layout.addWidget(label_slider)
group_layout.addWidget(slider)
self.elmtlayout.addLayout(group_layout)
def updateLabel(self, label, value):
label.setText(str(value/100))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="Dynamic GUI Builder")
parser.add_argument("param_file", help="Path to the text file containing parameters")
args = parser.parse_args()
with open(args.param_file, "r") as file:
# content = file.read()
lines = file.readlines()
groups = []
# matches = re.findall(r'#>\s+(\w+)\s+(\w+)=(.+?)(?:\n|#>|$)', content, re.DOTALL)
# for match in matches:
# group_type = match[0]
# group_name = match[1]
# options = match[2].split(' ', 1)
# default_option = []
# if len(options) > 1:
# default_option = options[0].split(',')
# options = options[1].split(',')
pattern = r"\s*(\w+)\s*=\s*([^\s#]+)\s*#\s*([^\#]+)\s*#\s*>\s*(\w+)(?:\s+(\S+))?"
for line in lines:
match = re.match(pattern, line)
if match:
group_type = match.group(4)
group_name = match.group(1)
default_option = match.group(2)
options = match.group(5).split(',') if match.group(5) else ""
help = match.group(3)
groups.append((group_type, group_name, options, default_option, help))
app = QtWidgets.QApplication(sys.argv)
w = MainWindow(groups)
w.show()
print(sys.argv)
try:
print('opening window')
sys.exit(app.exec())
except SystemExit:
print('closing window')