-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpyq.py
More file actions
128 lines (93 loc) · 4.36 KB
/
pyq.py
File metadata and controls
128 lines (93 loc) · 4.36 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
import sys
import random
from PyQt6.QtWidgets import QApplication, QMainWindow, QLabel, QPushButton, QVBoxLayout, QHBoxLayout, QWidget, QLineEdit, QTabWidget, QTextEdit
class MyWindow(QMainWindow):
def __init__(self):
super().__init__()
self.setWindowTitle("Simple GUI")
self.setGeometry(300, 300, 400, 200)
# Create a central widget and set it as the main window's central widget
central_widget = QWidget(self)
self.setCentralWidget(central_widget)
# Create a vertical layout
layout = QVBoxLayout(central_widget)
# Create a tab widget
self.tab_widget = QTabWidget(self)
# Create the first tab
self.tab1 = QWidget()
self.tab_widget.addTab(self.tab1, "Tab 1")
# Create a vertical layout for the first tab
tab1_layout = QVBoxLayout(self.tab1)
# Create a label
self.label = QLabel("Hello, PyQt6!", self)
# Create a horizontal layout for buttons and text input fields
button_layout = QHBoxLayout()
# Create buttons
self.button1 = QPushButton("Button 1", self)
self.button2 = QPushButton("Button 2", self)
# Create text input fields
self.input1 = QLineEdit(self)
self.input2 = QLineEdit(self)
# Add the buttons and text input fields to the horizontal layout
button_layout.addWidget(self.button1)
button_layout.addWidget(self.button2)
button_layout.addWidget(self.input1)
button_layout.addWidget(self.input2)
# Add the label and the horizontal layout to the vertical layout of the first tab
tab1_layout.addWidget(self.label)
tab1_layout.addLayout(button_layout)
# Create the second tab
self.tab2 = QWidget()
self.tab_widget.addTab(self.tab2, "Tab 2")
# Create a vertical layout for the second tab
tab2_layout = QVBoxLayout(self.tab2)
# Create a button to generate a random number
self.random_button = QPushButton("Generate Random Number", self)
self.random_label = QLabel(self)
# Add the button and label to the vertical layout of the second tab
tab2_layout.addWidget(self.random_button)
tab2_layout.addWidget(self.random_label)
# Create the third tab
self.tab3 = QWidget()
self.tab_widget.addTab(self.tab3, "Tab 3")
# Create a vertical layout for the third tab
tab3_layout = QVBoxLayout(self.tab3)
# Create a text edit widget to display the history
self.history_text = QTextEdit(self)
self.history_text.setReadOnly(True)
# Add the text edit widget to the vertical layout of the third tab
tab3_layout.addWidget(self.history_text)
# Connect the button signals to their respective slots
self.button1.clicked.connect(self.button1_clicked)
self.button2.clicked.connect(self.button2_clicked)
self.random_button.clicked.connect(self.generate_random_number)
# Add the tab widget to the main layout
layout.addWidget(self.tab_widget)
# List to store the history of generated numbers
self.number_history = []
def button1_clicked(self):
input_text = self.input1.text()
self.label.setText(f"Button 1 clicked!\nInput 1: {input_text}")
def button2_clicked(self):
input_text = self.input2.text()
self.label.setText(f"Button 2 clicked!\nInput 2: {input_text}")
def generate_random_number(self):
random_number = random.randint(0, 100)
self.random_label.setText(f"Random Number: {random_number}")
# Add the generated number to the history list
self.number_history.append(random_number)
# Limit the history to 25 last generated numbers
self.number_history = self.number_history[-25:]
# Update the history text display
self.update_history_text()
def update_history_text(self):
# Clear the history text
self.history_text.clear()
# Display the history of generated numbers
for number in self.number_history:
self.history_text.append(str(number))
if __name__ == "__main__":
app = QApplication(sys.argv)
window = MyWindow()
window.show()
sys.exit(app.exec())