-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathapp_nano.py
75 lines (56 loc) · 2.29 KB
/
app_nano.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
"""A very tiny app skeleton"""
import datetime
import random
import sys
from PySide6.QtCore import Qt, Signal
from PySide6.QtWidgets import (QApplication, QWidget, QVBoxLayout, QTextEdit, QPushButton,
QHBoxLayout)
class CustomWidget(QWidget):
"""A very simple custom widget"""
def __init__(self):
super().__init__()
# Set some initial properties
layout = QVBoxLayout()
self.setWindowTitle('Tiny sample app')
self.setLayout(layout)
# Add a text box
text_area = QTextEdit()
text_area.setAcceptRichText(False)
msg = 'Hello! Type here, or hit "Time to Text"'
text_area.setPlainText(msg)
layout.addWidget(text_area)
self.text_area = text_area
# Some controls at the bottom of the window
lower_row = QHBoxLayout()
lower_row.setContentsMargins(0, 0, 0, 0)
layout.addLayout(lower_row)
# Button for showing the time/some random data
time_to_text_btn = QPushButton('Time to Text')
# Add a tooltip (for on-hover)
time_to_text_btn.setToolTip('Show a sample message in the text box')
time_to_text_btn.clicked.connect(self.handle_press_time_to_text)
lower_row.addStretch(1) # Push the button to the right side
lower_row.addWidget(time_to_text_btn)
# Size the widget after adding stuff to the layout
self.resize(750, 500) # Resize children (if needed) below this line
# Make sure you show() the widget!
self.show()
def handle_press_time_to_text(self):
timestring = datetime.datetime.now().isoformat()
letters = [chr(codepoint) for codepoint in range(ord('A'), ord('A') + 26)]
some_rand_letters = random.choices(population=letters, k=4)
message = 'The time is: {}\nRandom letters: {}\n'.format(
timestring,
''.join(some_rand_letters)
)
self.text_area.setPlainText(message)
def run_gui():
"""Function scoped main app entrypoint"""
# Initialize the QApplication!
app = QApplication(sys.argv)
# This widget shows itself (the main GUI entrypoint)
my_widget = CustomWidget()
# Run the program/start the event loop with exec()
sys.exit(app.exec())
if __name__ == '__main__':
run_gui()