Hey guys, I got a little bored and thought I would to make some of the basic Client/Server applications I have a little bit flashier with a gui interface. So far I have just been working with the server and the main problem that I run into is when the program has to enter a loop while waiting for a response the gui starts to hang. The work around I have been using so far which quite frankly sucks, is to print a response while the exeption is being raised. It keeps the gui from hanging but it's quite annoying. The other thing I tried was to have it sleep for a short period of time but the gui seems to really hate that. Is there a more common or better way to handle this? Here is the code I'm working with at the moment, sorry I do relize it's pretty sloppy right now. I promise to clean it up a bit once I can get it functioning a little better :).

from tkinter import *
from threading import Thread
from time import sleep
import socket

class Gui():
    def __init__(self, master):

        self.running = 0    #not listening
        self.addr = ""
        self.conn = ""
        self.frame = Frame(master)
        self.frame.pack(side = LEFT, anchor = N)

        self.startb = Button(self.frame, text = "Start", command = self.startc)
        self.startb.pack(side = LEFT, anchor = N)

        self.stopb = Button(self.frame, text = "Stop", command = self.stopc)
        self.stopb.pack(side = LEFT, anchor = N)

        self.connectionl = Label(self.frame, text = "not connected")
        self.connectionl.pack(side = LEFT, anchor = SW)

    def startc(self):
        if self.running == 0:
            self.running += 1
            self.ls = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
            port = 9999
            self.ls.bind(('', port))
            print("Server listening on port %s" %port)
            while self.running != 0:
                    (self.conn, self.addr) = self.ls.accept()
                    print("client is at", self.addr[0], "on port", self.addr[1])
                    self.running = 0
                    print("attempting to connect")#hangs without this
            if self.conn != "":
                self.rc = ""
                while self.rc != "done":
                        self.rc = self.conn.recv(100).decode('utf-8')
                        print("got command", self.rc)
                        print("no info sent yet")#hangs without this
            self.running = 0
            print("Server not listening")

    def stopc(self):
        self.running = 0

root = Tk()
gui = Gui(root)

Don't know on TkIntering,
but if you're serious with your app, grab wxPython and MySQL-python. Make a client that will access remotely the mysql server, authenticate it and populate data/delete depending on whether they are admins or not

After that exercise, you will be fresh for a rest :)

This article has been dead for over six months. Start a new discussion instead.