Repository navigation
Expand file tree
/
Copy pathapp.py
More file actions
121 lines (98 loc) · 3.76 KB
/
Copy pathapp.py
File metadata and controls
121 lines (98 loc) · 3.76 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
from PySide6.QtWidgets import (
QApplication, QWidget, QVBoxLayout, QHBoxLayout,
QLabel, QScrollArea, QLineEdit, QPushButton, QFrame
)
from PySide6.QtCore import Qt, QTime
from PySide6.QtGui import QPixmap
import os
class MessageBubble(QWidget):
def __init__(self, text, is_user=True, username="User", time=None):
super().__init__()
layout = QHBoxLayout(self)
layout.setContentsMargins(5, 5, 5, 5)
# Lấy giờ hiện tại nếu không truyền vào
if time is None:
time = QTime.currentTime().toString("HH:mm")
# ==== Bubble ====
bubble = QFrame()
bubble_layout = QVBoxLayout(bubble)
bubble_layout.setContentsMargins(10, 5, 10, 5)
# Nội dung
lbl_text = QLabel(text)
lbl_text.setWordWrap(True)
lbl_text.setStyleSheet(
"background-color: lightblue; border-radius: 10px; padding: 6px;" if is_user
else "background-color: lightgray; border-radius: 10px; padding: 6px;"
)
lbl_text.setMaximumWidth(250)
# Thời gian
lbl_time = QLabel(time)
lbl_time.setStyleSheet("font-size: 9px; color: gray;")
lbl_time.setAlignment(Qt.AlignRight)
bubble_layout.addWidget(lbl_text)
bubble_layout.addWidget(lbl_time)
# ==== Layout hiển thị ====
if is_user:
# User: chỉ có bubble, không avatar
layout.addStretch()
layout.addWidget(bubble)
else:
# Bot: có avatar
avatar = QLabel()
avatar.setFixedSize(40, 40)
img_path = "bot.png"
if os.path.exists(img_path):
pix = QPixmap(img_path).scaled(40, 40, Qt.KeepAspectRatio, Qt.SmoothTransformation)
avatar.setPixmap(pix)
else:
avatar.setStyleSheet("background-color: #9E9E9E; border-radius: 20px;")
avatar.setText("🤖")
avatar.setAlignment(Qt.AlignCenter)
layout.addWidget(avatar)
layout.addWidget(bubble)
layout.addStretch()
class ChatWindow(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Chatbot")
self.setGeometry(200, 100, 500, 600)
# Khung cuộn
self.scroll = QScrollArea()
self.scroll.setWidgetResizable(True)
self.container = QWidget()
self.layout_messages = QVBoxLayout(self.container)
self.layout_messages.setAlignment(Qt.AlignTop)
self.scroll.setWidget(self.container)
# Input
self.input = QLineEdit()
self.input.setPlaceholderText("Nhập tin nhắn...")
self.input.returnPressed.connect(self.send_message)
self.btn_send = QPushButton("Send")
self.btn_send.clicked.connect(self.send_message)
input_layout = QHBoxLayout()
input_layout.addWidget(self.input)
input_layout.addWidget(self.btn_send)
# Main layout
main_layout = QVBoxLayout(self)
main_layout.addWidget(self.scroll)
main_layout.addLayout(input_layout)
def add_message(self, text, is_user=True):
msg = MessageBubble(
text,
is_user,
username="Bạn" if is_user else "Bot"
)
self.layout_messages.addWidget(msg)
self.scroll.verticalScrollBar().setValue(self.scroll.verticalScrollBar().maximum())
def send_message(self):
text = self.input.text().strip()
if text:
self.add_message(text, is_user=True)
self.input.clear()
# Bot reply demo
self.add_message("Tôi đã nhận được: " + text, is_user=False)
if __name__ == "__main__":
app = QApplication([])
win = ChatWindow()
win.show()
app.exec()