user interface - Python | How would I put this into multiple threads and allow the GUI to update? -


i'm working on sub alert system gui, problem i'm having gui freezes because i'm running loop checks chat.

how incorporate existing gui , chat code system gui won't freeze , textfield update console has.

# import resources # import re import socket import importlib tkinter import * modules.irccommands import *  recentsub= 'n/a'  # close application # def close_window():     frmmain.destroy()  # main application # def start(): # list info in shell # terminal.insert('1.0', 'subscriber alert ver. 1.5 | created & modified rubbixcube' + "\n") terminal.insert("end", 'important information:' + "\n") terminal.insert("end", 'host = ' + host + "\n") terminal.insert("end", 'port = ' + str(port) + "\n") c in chan:     terminal.insert("end", 'chan = ' + c + "\n") terminal.insert("end", '\n' + "\n") terminal.insert("end", 'chat:' + "\n") frmmain.update_idletasks  ## define basic functions ## def get_sender(msg):     result = ""     char in msg:         if (char == "!"):             break         if (char != ":"):             result += char     return result  def get_message(msg):     result = ""     = 3     length = len(msg)     while < length:         result += msg[i] + " "         += 1     result = result.lstrip(':')     return result  ## end helper functions ##  def parse_message(channel, user, msg):     if len(msg) >= 1:         msg = msg.split(' ')         frmmain.update_idletasks   con = socket.socket() con.connect((host, port))  send_pass(con, pass) send_nick(con, nick) c in chan:     join_channel(con, c)  data = ""   while true:     try:         data = data+con.recv(1024)         data_split = re.split("\r\n", data)         data = data_split.pop()          line in data_split:             #print(line)             #line = str.rstrip(line)             line = str.split(line)              # stay connected server #             if (len(line) >= 1):                 if (line[0] == 'ping'):                     send_pong(con, line[1])                  if (line[1] == 'privmsg'):                     sender = get_sender(line[0])                     message = get_message(line)                     channel = line[2]                     terminal.insert("end", sender + ": " + message + "\n")                     frmmain.update_idletasks                      # welcome new subs #                     if (sender == "rubbixcube"):                         def excutecommand(con, channel, user, message, ismod, issub):                             msg = str.split(message)                             if re.match('\w* subscribed \w* months in row!', message):                                 recentsub = msg[0]                                 print(recentsub)                                 send_message(con, channel, 'thanks continued contribution %s!' % msg[0])                             elif re.match('\w* subscribed!', message):                                 recentsub = msg[0]                                 print(recentsub)                                 send_message(con, channel, str.format('welcome channel %s. enjoy stay!' % msg[0]))                             elif (re.match('\w* viewers resubscribed while away!', message)):                                 send_message(con, channel, 'thanks subscribing!')                         excutecommand(con, channel, sender, message, false, false)                      parse_message(channel, sender, message)      except socket.error:         print("socket died")      except socket.timeout:        print("socket timeout")    ## gui ## # start loop of program frmmain = tk()  app = frame(frmmain) app.grid()  # configure gui frmmain.title('subscriber alert ver. 2.0') frmmain.geometry('455x357') frmmain.resizable(0,0)  # button start = button(app, text='     start     ') start['command'] = start start.grid(row=0, column=0, pady=5)  close = button(app, text='      exit       ') close['command'] = close_window close.grid(row=0, column=2, pady=5)   # label sub_lbl = label(app, text='most recent subscriber:') sub_lbl.grid(row=1, column=0, columnspan=2, pady=10, padx=5)  sub_lbl2 = label(app, text=recentsub) sub_lbl2.grid(row=1, column=1, columnspan=3, pady=10, padx=5)   # console terminal = text(app, width=56, height=17, wrap=word) terminal.grid(row=3, column=0, columnspan=3)   # end loop of program frmmain.mainloop() 

thanks

"gui-freezes issues" common questions network applications in python. either network socket or stream callign blocking or timeouts set long, pipes , io stream not finished being flushed or gui's reactor not setup work network io reactor (mainloop).

typical workarounds threading network io queue.queue(), or use network package supports events, guis event driven. twisted , asyncio offer event driven io. or provided gui packages , networking packages have "reactors" (mainloops) written work other specific packages. twisted has reactors tkinter , wx example.


Popular posts from this blog