-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRandom_Number_Generator-3-Mini.py
More file actions
189 lines (163 loc) · 7.35 KB
/
Random_Number_Generator-3-Mini.py
File metadata and controls
189 lines (163 loc) · 7.35 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
import sys
import random
from PyQt5.QtWidgets import (QApplication, QWidget, QVBoxLayout,
QPushButton, QLabel, QLineEdit,
QComboBox, QSpinBox, QGridLayout,
QMessageBox, QTextEdit)
from PyQt5.QtGui import QFont
class RandomGenerator(QWidget):
def __init__(self):
super().__init__()
self.is_dark_theme = True
self.history = []
self.initUI()
def initUI(self):
# Настройки окна
self.setWindowTitle('Генератор Случайных Чисел')
self.setGeometry(100, 100, 600, 700)
# Кнопки управления
self.theme_button = QPushButton("Светлая тема")
self.theme_button.clicked.connect(self.toggle_theme)
self.help_button = QPushButton("?")
self.help_button.clicked.connect(self.show_help)
self.export_button = QPushButton("Экспорт")
self.export_button.clicked.connect(self.export_history)
# Основной макет
layout = QGridLayout()
layout.addWidget(self.theme_button, 0, 0)
layout.addWidget(self.help_button, 0, 1)
layout.addWidget(self.export_button, 0, 2)
# Результат
self.result = QTextEdit()
self.result.setReadOnly(True)
self.result.setFont(QFont("Arial", 16))
layout.addWidget(self.result, 1, 0, 1, 5)
# Минимальное значение
layout.addWidget(QLabel('Минимальное значение:'), 2, 0)
self.min_input = QSpinBox()
self.min_input.setRange(-1000000, 1000000)
layout.addWidget(self.min_input, 2, 1)
# Максимальное значение
layout.addWidget(QLabel('Максимальное значение:'), 2, 2)
self.max_input = QSpinBox()
self.max_input.setRange(-1000000, 1000000)
self.max_input.setValue(100)
layout.addWidget(self.max_input, 2, 3)
# Тип генерации
layout.addWidget(QLabel('Тип генерации:'), 3, 0)
self.generation_type = QComboBox()
self.generation_type.addItems([
'Случайные числа',
'Уникальные числа',
'Взвешенная генерация'
])
layout.addWidget(self.generation_type, 3, 1)
# Тип числа
layout.addWidget(QLabel('Тип числа:'), 3, 2)
self.type_select = QComboBox()
self.type_select.addItems(['Целое', 'Вещественное'])
layout.addWidget(self.type_select, 3, 3)
# Количество
layout.addWidget(QLabel('Количество:'), 4, 0)
self.count_input = QSpinBox()
self.count_input.setRange(1, 1000)
self.count_input.setValue(1)
layout.addWidget(self.count_input, 4, 1)
# Кнопка генерации
self.generate_button = QPushButton("Сгенерировать")
self.generate_button.clicked.connect(self.generate_numbers)
layout.addWidget(self.generate_button, 5, 0, 1, 5)
self.setLayout(layout)
self.apply_theme()
def generate_numbers(self):
try:
min_val = self.min_input.value()
max_val = self.max_input.value()
count = self.count_input.value()
generation_type = self.generation_type.currentText()
num_type = 'int' if self.type_select.currentText() == 'Целое' else 'float'
if min_val > max_val:
self.result.setText("Минимальное значение не может быть больше максимального!")
return
if generation_type == 'Случайные числа':
if num_type == 'int':
numbers = [random.randint(min_val, max_val) for _ in range(count)]
else:
numbers = [round(random.uniform(min_val, max_val), 2) for _ in range(count)]
result_text = f"Сгенерированы числа ({count} шт.):\n{numbers}"
elif generation_type == 'Уникальные числа':
numbers = random.sample(range(min_val, max_val + 1), count)
result_text = f"Сгенерированы уникальные числа:\n{numbers}"
elif generation_type == 'Взвешенная генерация':
numbers = [1, 2, 3, 4, 5]
weights = [0.1, 0.2, 0.3, 0.2, 0.2]
weighted_numbers = random.choices(numbers, weights=weights, k=count)
result_text = f"Сгенерированы взвешенные числа:\n{weighted_numbers}"
self.result.setText(result_text)
self.history.append(result_text)
except Exception as e:
self.result.setText(f"Ошибка: {str(e)}")
def apply_theme(self):
if self.is_dark_theme:
self.setStyleSheet("""
background-color: #2E2E2E;
color: white;
""")
self.theme_button.setStyleSheet("""
background-color: #4C4C4C;
color: white;
""")
self.help_button.setStyleSheet("""
background-color: #4C4C4C;
color: white;
""")
self.export_button.setStyleSheet("""
background-color: #4C4C4C;
color: white;
""")
self.result.setStyleSheet("""
background-color: #1E1E1E;
color: white;
""")
else:
self.setStyleSheet("""
background-color: #FFFFFF;
color: black;
""")
self.theme_button.setStyleSheet("""
background-color: #CCCCCC;
color: black;
""")
self.help_button.setStyleSheet("""
background-color: #CCCCCC;
color: black;
""")
self.export_button.setStyleSheet("""
background-color: #CCCCCC;
color: black;
""")
self.result.setStyleSheet("""
background-color: #F0F0F0;
color: black;
""")
def toggle_theme(self):
self.is_dark_theme = not self.is_dark_theme
self.apply_theme()
def show_help(self):
QMessageBox.information(self, "Справка",
"Генератор случайных чисел:\n"
"1. Выберите минимальное и максимальное значения.\n"
"2. Выберите тип генерации.\n"
"3. Укажите количество чисел для генерации.\n" "4. Нажмите 'Сгенерировать' для получения результата.")
def export_history(self):
with open("history.txt", "w") as file:
for entry in self.history:
file.write(entry + "\n")
QMessageBox.information(self, "Экспорт", "История генерации экспортирована в history.txt")
def main():
app = QApplication(sys.argv)
generator = RandomGenerator()
generator.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()