Parcourir la source

Add multi-line chat

master
Noëlle Anthony il y a 4 ans
Parent
révision
dcaadfb5a3
3 fichiers modifiés avec 36 ajouts et 16 suppressions
  1. 2
    0
      README.md
  2. 2
    1
      config.ini
  3. 32
    15
      main.py

+ 2
- 0
README.md Voir le fichier

@@ -19,6 +19,8 @@ Set `sizew` and `sizeh` to set the width and height, respectively, of the window

List the `users` you'd like to include in the chat, separated by commas.

List `colors` for each user, using RGB hexadecimal codes (like #ff0000 for bright red). If a user isn't assigned a color, their text will be black (#000000).

### Usage

`> python3 main.py`

+ 2
- 1
config.ini Voir le fichier

@@ -3,4 +3,5 @@ timestamp = 1
logfile = chat.log
sizew = 640
sizeh = 480
users = MyName, MyOtherName
users = MyName, MyOtherName
colors = #cc0000, #00cc00

+ 32
- 15
main.py Voir le fichier

@@ -1,32 +1,46 @@
from PySide2 import QtWidgets as qt
from PySide2 import QtCore as qtc
from PySide2.QtGui import QColor
import configparser, sys, os
from datetime import datetime as dt

set_debug = False

class TextLine:
def __init__(self, username, textarea, timestamp, logfile):
def __init__(self, username, color, textarea, timestamp, logfile):
self.username = username
self.color = "#000000" if color == "" else color
self.qcolor = QColor(int(self.color[1:3], 16), int(self.color[3:5], 16), int(self.color[5:7], 16))
self.label = qt.QLabel("&{}:".format(username))
self.message = qt.QLineEdit()
self.message.returnPressed.connect(self.pressButton)
self.message = qt.QPlainTextEdit()
self.message.keyPressEvent = self.isItEnter
self.message.setMaximumSize(600, 40)
self.textarea = textarea
self.label.setBuddy(self.message)
self.timestamp = timestamp
self.logfile = logfile
def pressButton(self):
if self.timestamp:
t = dt.now()
o = "[{}] {}: {}".format(t.strftime("%H:%M:%S"), self.username, self.message.text())
def isItEnter(self, event):
if event.key() == qtc.Qt.Key_Enter or event.key() == qtc.Qt.Key_Return:
mod = qt.QApplication.keyboardModifiers()
if mod == qtc.Qt.ShiftModifier:
self.message.insertPlainText("\n")
else:
sentText = self.message.toPlainText()
sentText = sentText.replace("\n", "\t\n")
if self.timestamp:
t = dt.now()
o = "[{}] {}: {}".format(t.strftime("%H:%M:%S"), self.username, sentText)
else:
o = "{}: {}".format(self.username, sentText)
self.textarea.setTextColor(self.qcolor)
self.textarea.append(o)
if self.logfile != "":
with open(self.logfile, "a", encoding="utf8") as file:
file.write(o + "\n")
self.message.clear()
else:
o = "{}: {}".format(self.username, self.message.text())
self.textarea.append(o)
if self.logfile != "":
with open(self.logfile, "a", encoding="utf8") as file:
file.write(o + "\n")
self.message.clear()
qt.QPlainTextEdit.keyPressEvent(self.message, event)

def toString(self):
print(self.username)
@@ -52,10 +66,13 @@ def main():

debug("Getting users...")
users = [x.strip() for x in cfg["users"].split(",")]
colors = [x.strip() for x in cfg["colors"].split(",")]
while len(colors) < len(users):
colors.append("#000000")
debug("Users are {}".format(users))
inputs = []
for user in users:
ipt = TextLine(user, text_area, timestamp, cfg["logfile"])
for i in range(len(users)):
ipt = TextLine(users[i], colors[i], text_area, timestamp, cfg["logfile"])
inputs.append(ipt)
for ipt in inputs:
layout.addWidget(ipt.label)

Chargement…
Annuler
Enregistrer