選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

main.py 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. from PySide2 import QtWidgets as qt
  2. from PySide2 import QtCore as qtc
  3. from PySide2.QtGui import QColor
  4. import configparser, sys, os
  5. from datetime import datetime as dt
  6. set_debug = False
  7. class TextLine:
  8. def __init__(self, username, color, textarea, timestamp, logfile):
  9. self.username = username
  10. self.color = "#000000" if color == "" else color
  11. self.qcolor = QColor(int(self.color[1:3], 16), int(self.color[3:5], 16), int(self.color[5:7], 16))
  12. self.label = qt.QLabel("&{}:".format(username))
  13. self.message = qt.QPlainTextEdit()
  14. self.message.keyPressEvent = self.isItEnter
  15. self.message.setMaximumSize(600, 40)
  16. self.textarea = textarea
  17. self.label.setBuddy(self.message)
  18. self.timestamp = timestamp
  19. self.logfile = logfile
  20. def isItEnter(self, event):
  21. if event.key() == qtc.Qt.Key_Enter or event.key() == qtc.Qt.Key_Return:
  22. mod = qt.QApplication.keyboardModifiers()
  23. if mod == qtc.Qt.ShiftModifier:
  24. self.message.insertPlainText("\n")
  25. else:
  26. sentText = self.message.toPlainText()
  27. sentText = sentText.replace("\n", "\t\n")
  28. if self.timestamp:
  29. t = dt.now()
  30. o = "[{}] {}: {}".format(t.strftime("%H:%M:%S"), self.username, sentText)
  31. else:
  32. o = "{}: {}".format(self.username, sentText)
  33. self.textarea.setTextColor(self.qcolor)
  34. self.textarea.append(o)
  35. if self.logfile != "":
  36. with open(self.logfile, "a", encoding="utf8") as file:
  37. file.write(o + "\n")
  38. self.message.clear()
  39. else:
  40. qt.QPlainTextEdit.keyPressEvent(self.message, event)
  41. def toString(self):
  42. print(self.username)
  43. def debug(message):
  44. if set_debug:
  45. print(message)
  46. def main():
  47. debug("Loading config...")
  48. cfg = config()
  49. timestamp = True if int(cfg["timestamp"]) == 1 else False
  50. debug("Setting up...")
  51. app = qt.QApplication([])
  52. layout = qt.QVBoxLayout()
  53. debug("Making text area...")
  54. text_area = qt.QTextEdit()
  55. text_area.setFocusPolicy(qtc.Qt.NoFocus)
  56. layout.addWidget(text_area)
  57. debug("Getting users...")
  58. users = [x.strip() for x in cfg["users"].split(",")]
  59. colors = [x.strip() for x in cfg["colors"].split(",")]
  60. while len(colors) < len(users):
  61. colors.append("#000000")
  62. debug("Users are {}".format(users))
  63. inputs = []
  64. for i in range(len(users)):
  65. ipt = TextLine(users[i], colors[i], text_area, timestamp, cfg["logfile"])
  66. inputs.append(ipt)
  67. for ipt in inputs:
  68. layout.addWidget(ipt.label)
  69. layout.addWidget(ipt.message)
  70. debug("Starting window...")
  71. window = qt.QWidget()
  72. window.setLayout(layout)
  73. window.resize(int(cfg["sizew"]), int(cfg["sizeh"]))
  74. window.show()
  75. debug("Starting interactivity...")
  76. app.exec_()
  77. def config():
  78. config = configparser.ConfigParser()
  79. if not os.path.isfile('config.ini'):
  80. print("config.ini is missing!")
  81. sys.exit(0)
  82. config.read('config.ini')
  83. return config["DEFAULT"]
  84. if __name__ == "__main__":
  85. main()