python - Tkinter and Threading -
i kind of new python , wrote client server based program. client side using tkinter gui , when program enters function communicates server , runs whole program gui freezes. know why happens have no idea how fix in specific program..
from tkinter import * import re import os import socket import os.path import time maxsize=5000000000 # bigest file u can send class application(frame): gateway =socket.socket(socket.af_inet, socket.sock_stream) def __init__(self, master): frame.__init__(self, master) self.grid() self.creat_widgets() def creat_widgets(self): self.instruction1 = label(self,text = "enter ip address:") self.instruction1.grid(row = 0, column = 0, columnspan = 2, sticky = w) self.ipentry = entry(self) self.ipentry.grid(row = 0, column = 1, sticky = w + e) self.instruction2 = label(self, text = "enter port:") self.instruction2.grid(row = 1, column = 0, columnspan = 2, sticky = w) self.portentry = entry(self) self.portentry.grid(row = 1, column = 1, sticky = w + e) self.instruction3 = label(self,text = "enter path:") self.instruction3.grid(row = 2, column = 0, columnspan = 2, sticky = w) self.pathentry = entry(self) self.pathentry.grid(row = 2, column = 1, sticky = w + e) self.send_button = button(self, text = "send file" ,command = self.send_data) self.send_button.grid(row = 3, column = 0, sticky = w) self.inbox = text(self, width = 40, height = 5, wrap = word) self.inbox.grid(row =5,column = 0 , columnspan= 3) def send_data(self): content = self.ipentry.get() if(re.match("^(?:[0-9]{1,3}\.){3}[0-9]{1,3}$",content)): self.ip = content else: self.ip = false content = self.portentry.get() if(re.match("^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$",content)): self.port = content else: self.port = false content = self.pathentry.get() if os.path.exists(content): self.path = content else: self.path = false if (self.ip , self.port , self.path): self.connect() if os.path.getsize(self.path)<=maxsize: self.update_text("sending file......") self.sendfile() else: self.update_text("file big (max size 500mb)") else: self.update_text("error entrys") def connect(self): try: self.gateway.connect((self.ip, int(self.port))) except socket.error, msg: message = 'failed create socket. error code: ' + str(msg[0]) + ' , error message : ' + msg[1] self.update_text(message) def sendfile(self):#send file in path entered self.update_text("sending file......") tail = os.path.split(self.path)#get files name , extention , send server. self.gateway.sendall(tail[1]) self.sendlen() try: open(str(self.path), 'rb') f: data = f.read() self.gateway.sendall(data) except exception, e: print e if self.chkprot(self.gateway.recv(10)): self.update_text("done!") def sendlen(self):#function handles the sending of length of file. flen=os.path.getsize(str(self.path)) self.gateway.sendall(str(flen)) while self.chkprot(self.gateway.recv(10))==false:#checks length has been sent , received , no problems occurred. self.gateway.sendall(str(flen)) #resend file length if problem occurred. def chkprot(self,num):#received protocol server sent , act accordingly. if num=="101": return true if num=="102": print "an error has occured" return false elif num=="103": return false def update_text(self, etext): # self.inbox.delete(0.0, end) self.inbox.insert(0.0, etext) root = tk() root.title("hcff") root.geometry("300x200") app = application(root) root.mainloop()
to avoid freezing interface, can run send_data
function in separate thread (using threading module). import , change 1 line inside create_widgets
function:
import threading ... def creat_widgets(self): .... self.send_button = button(self, text = "send file", command = lambda: threading.thread(target=self.send_data).start())
Comments
Post a Comment