-
Notifications
You must be signed in to change notification settings - Fork 2
/
get_text_input.py
62 lines (49 loc) · 1.61 KB
/
get_text_input.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
import sys
from PyQt6.QtGui import QKeyEvent
from PyQt6.QtWidgets import (
QApplication,
QMainWindow,
QLineEdit,
QPushButton,
QVBoxLayout,
QWidget,
QLabel,
)
from PyQt6.QtCore import Qt
class TextInputCapture(QWidget):
def __init__(self, callback=None):
super().__init__()
# Set up the main window
self.setWindowTitle("Enter API Key")
self.setGeometry(100, 100, 300, 100)
# Set up layout
layout = QVBoxLayout()
description_label = QLabel("Enter your OpenAI API key:")
# Create a QLineEdit widget
self.input_field = QLineEdit(self)
self.input_field.setPlaceholderText("Enter some text here")
# Create a button
self.button = QPushButton("Submit", self)
self.button.clicked.connect(self.get_text)
# Add widgets to layout
layout.addWidget(description_label)
layout.addWidget(self.input_field)
layout.addWidget(self.button)
self.callback = callback
# Set central widget
self.setLayout(layout)
def keyPressEvent(self, event: QKeyEvent | None) -> None:
if event.key() == Qt.Key.Key_Return:
self.get_text()
def get_text(self):
# Capture text from QLineEdit and store it in a variable
user_text = self.input_field.text()
if self.callback is not None:
self.callback(user_text)
self.close()
# Now you can use the variable user_text as needed
if __name__ == "__main__":
app = QApplication(sys.argv)
window = TextInputCapture()
window.show()
sys.exit(app.exec())