Browse Source

First usable version of the app

master
Noëlle Anthony 4 years ago
parent
commit
994e9147d0
3 changed files with 116 additions and 0 deletions
  1. 22
    0
      README.md
  2. 6
    0
      config.ini
  3. 88
    0
      main.py

+ 22
- 0
README.md View File

@@ -1,2 +1,24 @@
# localchat

A Python 3 *local-only* chat client, for when multiple people at the same computer want to communicate quickly.

### Requirements

* Python 3.5+
* PySide2 (`pip3 install PySide2`)

### Config

Edit `config.ini` to change the app's behavior.

Set `timestamp=1` to add HH:MM:SS timestamps to the chat window, `=0` to not use timestamps.

Set `logfile=<filename>` to log each line of chat to the specified file. Remove the filename (i.e. `logfile=`) to disable logging.

Set `sizew` and `sizeh` to set the width and height, respectively, of the window in pixels.

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

### Usage

Each user gets their own input line at the bottom of the window. Pressing `Enter` in a given input line will send that line (and *only* that line, not any of the others) to the chat window. Easy as that!

+ 6
- 0
config.ini View File

@@ -0,0 +1,6 @@
[DEFAULT]
timestamp = 1
logfile = chat.log
sizew = 640
sizeh = 480
users = MyName, MyOtherName

+ 88
- 0
main.py View File

@@ -0,0 +1,88 @@
from PySide2 import QtWidgets as qt
from PySide2 import QtCore as qtc
import configparser, sys, os
from datetime import datetime as dt

set_debug = False

class TextLine:
def __init__(self, username, textarea, timestamp, logfile):
self.username = username
self.label = qt.QLabel("&{}:".format(username))
self.message = qt.QLineEdit()
self.message.returnPressed.connect(self.pressButton)
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())
else:
o = "{}: {}".format(self.username, self.message.text())
self.textarea.append(o)
if self.logfile != "":
with open(self.logfile, "a") as file:
file.write(o + "\n")
self.message.clear()

def toString(self):
print(self.username)

def debug(message):
if set_debug:
print(message)

def main():
debug("Loading config...")
cfg = config()

timestamp = True if int(cfg["timestamp"]) == 1 else False

debug("Setting up...")
app = qt.QApplication([])
layout = qt.QVBoxLayout()

debug("Making text area...")
text_area = qt.QTextEdit()
text_area.setFocusPolicy(qtc.Qt.NoFocus)
layout.addWidget(text_area)

debug("Getting users...")
users = [x.strip() for x in cfg["users"].split(",")]
debug("Users are {}".format(users))
inputs = []
for user in users:
ipt = TextLine(user, text_area, timestamp, cfg["logfile"])
inputs.append(ipt)
for ipt in inputs:
layout.addWidget(ipt.label)
layout.addWidget(ipt.message)

debug("Starting window...")
window = qt.QWidget()
window.setLayout(layout)
window.resize(int(cfg["sizew"]), int(cfg["sizeh"]))
window.show()

debug("Starting interactivity...")
app.exec_()

def send_message(ipt, text_area):
debug("Sending message!")
text_area.append("{}: {}".format(ipt.username, ipt.message.text()))
ipt.message.clear()

def config():
config = configparser.ConfigParser()
if not os.path.isfile('config.ini'):
print("config.ini is missing!")
sys.exit(0)
config.read('config.ini')
return config["DEFAULT"]

if __name__ == "__main__":
main()

Loading…
Cancel
Save