2022-10-28 14:17:00 +02:00
import ast
import communicate
import tkinter as tk
from tkinter import messagebox
import threading
import tkinter . ttk as ttk
2022-10-30 14:49:29 +01:00
2022-10-28 14:17:00 +02:00
file = open ( " config.config " , ' r ' )
file = file . read ( )
config = ast . literal_eval ( file )
sessionid = " "
2022-10-30 14:49:29 +01:00
global conversations
2022-10-28 14:17:00 +02:00
conversations = [ ]
def updateconfig ( ) :
global config
file = open ( " config.config " , ' w ' )
file . write ( str ( config ) )
file . close ( )
def registeeer ( name , password ) :
global container
global sessionid
name = name . get ( )
password = password . get ( )
2022-10-30 14:49:29 +01:00
back = communicate . get ( config [ " serveraddress " ] + " : " + config [ " port " ] , { " targetserver " : " python messages main server " , " targetfunction " : " register " , " name " : name , " pass " : password } )
2022-10-28 14:17:00 +02:00
if back [ 0 ] == " " :
messagebox . showinfo ( " erfolgreich! " , " sie haben sich erfolgreich registriert! " )
config [ " logindata " ] [ " name " ] = name
config [ " logindata " ] [ " pass " ] = password
updateconfig ( )
sessionid = back
container . destroy ( )
else :
messagebox . showerror ( " fehler! " , " kann nicht registrieren: name wird schon benutzt " )
def register ( ) :
global container
container . destroy ( )
container = tk . Tk ( " registrieren " )
namevar = tk . StringVar ( container )
passvar = tk . StringVar ( container )
nameentry = tk . Entry ( container , textvariable = namevar )
passentry = tk . Entry ( container , textvariable = passvar )
registerbutton = tk . Button ( container , text = " registrieren " , command = lambda : registeeer ( namevar , passvar ) )
namelabel = tk . Label ( container , text = " Name " )
passlabel = tk . Label ( container , text = " Passwort " )
namelabel . grid ( )
nameentry . grid ( )
passlabel . grid ( )
passentry . grid ( )
registerbutton . grid ( )
container . mainloop ( )
def loginnn ( name , password ) :
global container
global sessionid
name = name . get ( )
password = password . get ( )
2022-10-30 14:49:29 +01:00
back = communicate . get ( config [ " serveraddress " ] + " : " + config [ " port " ] , { " targetserver " : " python messages main server " , " targetfunction " : " getsessionid " , " name " : name , " pass " : password } )
2022-10-28 14:17:00 +02:00
if back [ 0 ] == " " :
messagebox . showinfo ( " erfolgreich! " , " sie haben sich erfolgreich angemeldet! " )
config [ " logindata " ] [ " name " ] = name
config [ " logindata " ] [ " pass " ] = password
updateconfig ( )
sessionid = back
container . destroy ( )
else :
messagebox . showerror ( " fehler! " , " kann nicht anmelden: falscher name oder passwort " )
def login ( ) :
global container
container . destroy ( )
container = tk . Tk ( " anmelden " )
namevar = tk . StringVar ( container )
passvar = tk . StringVar ( container )
nameentry = tk . Entry ( container , textvariable = namevar )
passentry = tk . Entry ( container , textvariable = passvar )
registerbutton = tk . Button ( container , text = " anmelden " , command = lambda : loginnn ( namevar , passvar ) )
namelabel = tk . Label ( container , text = " Name " )
passlabel = tk . Label ( container , text = " Passwort " )
namelabel . grid ( )
nameentry . grid ( )
passlabel . grid ( )
passentry . grid ( )
registerbutton . grid ( )
container . mainloop ( )
def send ( message , conversation ) :
global sessionid
message = message . get ( )
2022-10-30 14:49:29 +01:00
communicate . patch ( config [ " serveraddress " ] + " : " + config [ " port " ] , { " targetserver " : " python messages main server " , " targetfunction " : " send " , " sessionid " : sessionid , " message " : message , " conversation " : conversation } )
2022-10-28 14:17:00 +02:00
def turnback ( target ) :
temp = " "
for i in target :
temp = temp + i + " \n "
return temp
def showconversation ( conversation2 ) :
global conversations
container2 = tk . Tk ( " conversation " )
conversationtext = conversations [ conversation2 ] [ " conversation " ]
conversation = tk . Text ( container2 )
conversation . insert ( tk . END , conversationtext )
conversation . config ( state = tk . DISABLED )
conversation . grid ( padx = 2 , pady = 2 )
scrollbar = ttk . Scrollbar ( container2 , command = conversation . yview )
scrollbar . grid ( column = 3 )
conversation [ ' yscrollcommand ' ] = scrollbar . set
sendvar = tk . StringVar ( container2 )
entry = tk . Entry ( container2 , textvariable = sendvar )
sendbutton = tk . Button ( container2 , command = lambda : send ( sendvar , conversations [ conversation2 ] [ " speakers " ] ) , text = " senden " )
entry . grid ( )
sendbutton . grid ( )
while True :
2022-10-30 14:49:29 +01:00
temp = communicate . get ( config [ " serveraddress " ] + " : " + config [ " port " ] , { " targetserver " : " python messages main server " , " targetfunction " : " update " , " sessionid " : sessionid } ) [ conversation2 ] [ " conversation " ] . split ( " \n " )
2022-10-28 14:17:00 +02:00
for i in conversationtext . split ( " \n " ) :
temp . remove ( i )
if len ( temp ) != 0 :
conversation . grid_forget ( )
conversation . config ( state = tk . NORMAL )
conversation . insert ( tk . END , turnback ( temp ) )
conversation . config ( state = tk . DISABLED )
conversation . grid ( row = 0 )
conversationtext = conversationtext + turnback ( temp )
container2 . update ( )
def updatescreen ( ) :
global container
global conversations
global conversationbuttons
global config
2022-10-30 14:49:29 +01:00
if True :
2022-10-28 14:17:00 +02:00
for temp in range ( len ( conversations ) ) :
2022-10-30 14:49:29 +01:00
temp2 = conversations [ temp ] [ " speakers " ] . split ( " <splitedaccountname> " )
2022-10-28 14:17:00 +02:00
temp2 . remove ( config [ " logindata " ] [ " name " ] )
temp2 = temp2 [ 0 ]
if not temp2 in conversationbuttons :
2022-10-30 14:49:29 +01:00
conversationbuttons [ temp2 ] = tk . Button ( container , text = temp2 , command = lambda temp = temp : showconversation ( temp ) )
2022-10-28 14:17:00 +02:00
conversationbuttons [ temp2 ] . grid ( )
def update ( ) :
global sessionid
global conversations
while True :
2022-10-30 14:49:29 +01:00
conversations = communicate . get ( config [ " serveraddress " ] + " : " + config [ " port " ] , { " targetserver " : " python messages main server " , " targetfunction " : " update " , " sessionid " : sessionid } )
2022-10-28 14:17:00 +02:00
def newww ( name , container2 ) :
global sessionid
name = name . get ( )
2022-10-30 14:49:29 +01:00
communicate . patch ( config [ " serveraddress " ] + " : " + config [ " port " ] , { " targetserver " : " python messages main server " , " targetfunction " : " newconv " , " sessionid " : sessionid , " target " : name } )
2022-10-28 14:17:00 +02:00
container2 . destroy ( )
def newconversation ( ) :
container2 = tk . Tk ( " new conversation " )
namelabel = tk . Label ( container2 , text = " name des benutzers: " )
namelabel . grid ( )
stringvar = tk . StringVar ( container2 )
entry = tk . Entry ( container2 , textvariable = stringvar )
entry . grid ( )
gobutton = tk . Button ( container2 , text = " konversation erstellen " , command = lambda : newww ( stringvar , container2 ) )
gobutton . grid ( )
container2 . mainloop ( )
if config [ " logindata " ] [ " name " ] == None :
container = tk . Tk ( " PM " )
messagebox . showinfo ( " nicht angemeldet! " , " sie sind nicht angemeldet bitte melden sie sich an! " )
registerbutton = tk . Button ( container , text = " registrieren " , command = register )
loginbutton = tk . Button ( container , text = " anmelden " , command = login )
loginbutton . grid ( )
registerbutton . grid ( )
container . mainloop ( )
else :
2022-10-30 14:49:29 +01:00
sessionid = communicate . get ( config [ " serveraddress " ] + " : " + config [ " port " ] , { " targetserver " : " python messages main server " , " targetfunction " : " getsessionid " , " name " : config [ " logindata " ] [ " name " ] , " pass " : config [ " logindata " ] [ " pass " ] } )
2022-10-28 14:17:00 +02:00
conversationbuttons = { }
updatethread = threading . Thread ( target = update )
container = tk . Tk ( " PM " )
updatethread . start ( )
newconversationbutton = tk . Button ( container , text = " neue konversation " , command = newconversation )
newconversationbutton . grid ( )
2022-10-30 14:49:29 +01:00
while True :
container . update ( )
updatescreen ( )