0

I'm working on an application that utilizes sockets and tkinter (currently working on the server-side). I have encountered a problem: nothing happens, and I receive no error message, although IDLE claims the program is running.

My code is as follows:

import socket
import time # needed to prevent client data from overlapping
import tkinter as tk
from tkinter import *

host='127.0.0.1'
port=XXXXX # host and port for socket (locally hosted, at the moment)

serverSocket=socket.socket()
serverSocket.bind((host,port))# creates and binds a socket

a=tk.Tk()
a.title('Custodian Alert Server')
a.geometry('100x150') # opens tkinter window

loop=2 # used for closing part of the program (later)

def Clear(): # function to clear tkinter window of widgets
    for widget in a.winfo_children():
        widget.destroy()

def Home(): # creates homescreen to either start server, or exit the program
    Clear()
    Label(a,text='Welcome to the Custodian Alert Server.').pack()
    Label(a,text='').pack()
    b1=tk.Button(a,text='Turn On Server',command=Online())
    b1.pack()
    b2=tk.Button(a,text='Exit Program',command=Exit())
    b2.pack()
    a.mainloop() # pretty sure its either this, or the one below

def Online(): # creates screen that allows the server to receive data
    Clear()
    loop=1
    def Return():
        loop=2
    Label(a,text='Server is now online. \nAwaiting tickets...').pack()
    b3=tk.Button(a,text='Turn Off Server',command=Return())
    b3.pack()
    while loop==1:
        serverSocket.listen(1) # pretty sure its either this, or the one above
        conn, addr=serverSocket.accept()
        Receive()
    conn.close()
    Home()

def Receive(): # opens a new window which displays data retrieved from clients
    c=tk.Tk()
    c.title('New Ticket')
    c.geometry('100x100')
    data1=conn.recv(1024).decode()
    Label(c,text='Bathroom Location: ' + data1).pack()
    time.sleep(0.1)
    data2=conn.recv(1024).decode()
    Label(c,text='Bathroom Gender: ' + data2).pack()
    time.sleep(0.1)
    data3=conn.recv(1024).decode()
    Label(c,text='Bathroom Issue: ' + data3).pack()
    b4=tk.Button(c,text='Dismiss',command=c.destroy())
    b4.pack()

Home() # runs function which opens home screen

Based on my knowledge of tkinter and sockets, I have a feeling it's either the a.mainloop() which is waiting for something to happen with tkinter, or the serverSocket.listen(1) which is waiting for a client to connect.

However, I don't know for sure and don't know what I'd do regardless if I found out the error. Any ideas?

McJohnalds
  • 85
  • 6
  • You should use `command=Online` instead of `command=Online()` (same for other `command` options). Also you should consider using thread for listening connections and reading from socket. – acw1668 Mar 10 '20 at 01:06
  • In addition to the issue with the `Button`s' `command` argument, the infinite loop you have in your `Online()` function will make your app "hang" since it interfers with tkinter's `mainloop()`. You need to use the universal widget [`after()`](https://web.archive.org/web/20190222214221id_/http://infohost.nmt.edu/tcc/help/pubs/tkinter/web/universal.html) method to periodically call a function which polls the socket in a non-blocking way. – martineau Mar 10 '20 at 02:35

0 Answers0