master
Sparklight 3 years ago
parent 325e514ab3
commit 3fe2c32798

@ -1,39 +1,66 @@
import tkinter as tk
from tkinter import ttk
class ChatApp:
def __init__(self, master):
self.master = master
master.title("Chat App")
master.geometry("500x600")
self.chat_history = tk.Text(master, height=25, width=60, state="disabled")
self.chat_history.tag_configure("right", justify="right")
self.chat_history.tag_configure("left", justify="left")
self.chat_history.pack(side="top", fill="both", expand=True)
self.message_input = tk.Entry(master, width=60)
self.message_input.pack(side="left", padx=5, pady=5, expand=True, fill="both")
send_button_image = tk.PhotoImage(file="send.png")
self.send_button = tk.Button(master, image=send_button_image, command=self.send_message)
self.send_button.image = send_button_image
self.send_button.pack(side="right", padx=5, pady=5)
self.message_input.bind("<Return>", self.send_message)
def send_message(self, event=None):
message = self.message_input.get()
self.message_input.delete(0, "end")
self.display_message("Me", message, "right")
def display_message(self, name, message, justify):
self.chat_history.configure(state="normal")
self.chat_history.insert("end", f"{name}:\n", justify)
self.chat_history.insert("end", f"{message}\n")
self.chat_history.configure(state="disabled")
if __name__ == '__main__':
root = tk.Tk()
chat_app = ChatApp(root)
root.mainloop()
import sys
import requests
from PyQt5.QtWidgets import QApplication, QMainWindow, QGridLayout, QTextEdit, QPushButton, QLineEdit, QWidget
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Set window title
self.setWindowTitle("Chatbot")
# Set window size
self.setGeometry(100, 100, 800, 600)
# Create central widget
central_widget = QWidget()
# Create text input field for user's messages
self.text_input = QLineEdit(self)
self.text_input.setGeometry(20, 540, 550, 40)
# Create send button to send user's messages
self.send_button = QPushButton("Send", self)
self.send_button.setGeometry(580, 540, 200, 40)
self.send_button.clicked.connect(self.send_message)
# Create chat window to display conversation
self.chat_window = QTextEdit(self)
self.chat_window.setGeometry(20, 20, 760, 500)
self.chat_window.setReadOnly(True)
# Initialize message counter
self.num_messages = 0
def send_message(self):
# Get user's message from text input field
user_message = self.text_input.text()
# 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