master
Sparklight 3 years ago
parent 325e514ab3
commit 3fe2c32798

@ -1,39 +1,66 @@
import tkinter as tk import sys
from tkinter import ttk import requests
from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QTextEdit, QPushButton, QLineEdit, QWidget
class ChatApp:
def __init__(self, master):
self.master = master class MainWindow(QMainWindow):
master.title("Chat App") def __init__(self):
master.geometry("500x600") super().__init__()
self.chat_history = tk.Text(master, height=25, width=60, state="disabled") # Set window title
self.chat_history.tag_configure("right", justify="right") self.setWindowTitle("Chatbot")
self.chat_history.tag_configure("left", justify="left")
self.chat_history.pack(side="top", fill="both", expand=True) # Set window size
self.setGeometry(100, 100, 800, 600)
self.message_input = tk.Entry(master, width=60)
self.message_input.pack(side="left", padx=5, pady=5, expand=True, fill="both") # Create central widget
central_widget = QWidget()
send_button_image = tk.PhotoImage(file="send.png")
self.send_button = tk.Button(master, image=send_button_image, command=self.send_message) # Create text input field for user's messages
self.send_button.image = send_button_image self.text_input = QLineEdit(self)
self.send_button.pack(side="right", padx=5, pady=5) self.text_input.setGeometry(20, 540, 550, 40)
self.message_input.bind("<Return>", self.send_message) # Create send button to send user's messages
self.send_button = QPushButton("Send", self)
def send_message(self, event=None): self.send_button.setGeometry(580, 540, 200, 40)
message = self.message_input.get() self.send_button.clicked.connect(self.send_message)
self.message_input.delete(0, "end")
self.display_message("Me", message, "right") # Create chat window to display conversation
self.chat_window = QTextEdit(self)
def display_message(self, name, message, justify): self.chat_window.setGeometry(20, 20, 760, 500)
self.chat_history.configure(state="normal") self.chat_window.setReadOnly(True)
self.chat_history.insert("end", f"{name}:\n", justify)
self.chat_history.insert("end", f"{message}\n") # Initialize message counter
self.chat_history.configure(state="disabled") self.num_messages = 0
if __name__ == '__main__': def send_message(self):
root = tk.Tk() # Get user's message from text input field
chat_app = ChatApp(root) user_message = self.text_input.text()
root.mainloop()
# Increment message counter
self.num_messages += 1
# Send greeting to user
bot_response = f"Hello, you have sent {self.num_messages} messages."
# Add user's message and bot's response to chat window
self.chat_window.append(f"<p style='color: #0084ff;'><strong>You:</strong> {user_message}</p>")
self.chat_window.append(f"<p style='color: #008000;'><strong>Bot:</strong> {bot_response}</p>")
# Clear text input field
self.text_input.clear()
if __name__ == "__main__":
# Create QApplication instance
app = QApplication(sys.argv)
# Create MainWindow instance
main_window = MainWindow()
# Show MainWindow
main_window.show()
# Execute QApplication event loop
sys.exit(app.exec_())

Loading…
Cancel
Save