0

So today I've been working on a new GUI with tkinter and came across and issue with parsing a string from an entry into an int and I get this error.

Traceback (most recent call last):
  File "/Users/g.shiner21/Desktop/Python/P2PFile/P2PFileServer.py", line 47, in <module>
    bStartServer = Button(f1, text="Start Server", command=startServer(host))
  File "/Users/g.shiner21/Desktop/Python/P2PFile/P2PFileServer.py", line 28, in startServer
    intport = int(port)
ValueError: invalid literal for int() with base 10: ''

Here is the function,

def startServer(host):
    port = ePort.get()
    intport = int(port)
    s = socket.socket()
    s.bind((host, intport))
    s.listen(4)

The most confusing part is that when I test something like this,

strTest = "10000"
intTest = int(strTest)

I get no errors. So I don't think I'm doing my parsing wrong. But at this point I'm not too sure. Any help would be greatly appreciated! :)

Edit: For those that it helps here is the full program(or at least what I have so far. I commented out the second part of it while I was building the GUI because I didn't want to have to deal with any interferences.

# server.py

import socket                   # Import socket module
import sys
from tkinter import *
from tkinter import ttk

port = 0                  # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = "0.0.0.0" 

root=Tk()
root.title("Test Window")

c = '#ececec'

n = ttk.Notebook(root)  #Create notebook
f1 = ttk.Frame(n)   #Create frames for notebook
f2 = ttk.Frame(n)
f3 = ttk.Frame(n)
n.add(f1, text='Server')    #Add frames to notebook
n.add(f2, text='File')
n.add(f3, text='Playback')
n.pack(expand=1, fill='both')   #Pack notebook

def startServer(host):
    port = ePort.get()
    intport = int(port)
    s = socket.socket()
    s.bind((host, intport))
    s.listen(4)

lservType = Label(f1, text="Select Server Type: ", bg=c)
lservType.grid(columnspan=2, row=0)

R1 = Radiobutton(f1, text="Local Host", variable=host, value="localhost", bg=c)
R1.grid(row=1, column=0)
R2 = Radiobutton(f1, text="Wireless Host", variable=host, value="0.0.0.0", bg=c)
R2.grid(row=1, column=1)

lPort = Label(f1, text="Port: ", bg=c)
ePort = Entry(f1)

lPort.grid(row=2, column=0, sticky='e')
ePort.grid(row=2, column=1)

bStartServer = Button(f1, text="Start Server", command=startServer(host))
bStartServer.grid(row=3, columnspan=2)

"""
port = 60000                    # Reserve a port for your service.
s = socket.socket()             # Create a socket object
host = "0.0.0.0"                # Get local machine name
s.bind((host, port))            # Bind to the port
s.listen(4)                     # Now wait for client connection.
#print("Your IP address is: " + socket.gethostbyname(socket.gethostname()))

print ('Server listening....')

while True:
    conn, addr = s.accept()     # Establish connection with client.
`    print ('Got connection from', addr)
    input ('Press the enter key to send')

    filename = 'send.mov'
    print ('File Assigned')
    f = open(filename,'rb')
    print ('File Oppened')
    l = f.read(131072)
    print ('Reading')
    while (l):
        conn.send(l)
        print('Sent ',repr(l))
        l = f.read(131072)
        print ('Reading')
    f.close()

    print('Done sending')
    conn.close()
#sys.exit()
"""
root.mainloop()
gman1230321
  • 105
  • 1
  • 2
  • 10

1 Answers1

1

You get this error when the python can't cast the variable to int. make sure "port" is an integer and the try casting that to int. the second example works fine because your hard coding the value of input and it's an integer. You can update your function as follows:

def startServer(host):
    try:
        port = ePort.get()
        intport = int(port)
        s = socket.socket()
        s.bind((host, intport))
        s.listen(4)
    except BaseException:
        print('wrong entry')
reza karimi
  • 164
  • 1
  • 8
  • but that could be an empty string or a float number or not even a number. have you tried printing out port before casting it to int? – reza karimi May 20 '18 at 22:41
  • Also `port` can't be an int because toking entries only store the data as a string – gman1230321 May 20 '18 at 22:47
  • Yes I know that. but you're trying to cast it to int and store it in a variable called "intport". – reza karimi May 20 '18 at 22:50
  • 1
    What if I make a try, except statement, that will try it but will do nothing if it doesn't work, will python retry it once the function is called? – gman1230321 May 20 '18 at 22:59
  • Using try, except statement is definitely a good idea since it avoids your program from crashing when the entered value is not as expected. But python doesn't automatically call the function until it works just fine. Your function is only called when the button is pressed. So if the value of Entry field is as expected, the function will work fine. – reza karimi May 20 '18 at 23:03
  • Well I tried it and it didn’t seem to work. In the except, i put in a print. Once the program started, it printed(as expected) I also put a print command in the try part. However once I put in a number into the entry and hit the button, nothing happened. This probably means that it wasn’t able to try the function again for some reason. – gman1230321 May 20 '18 at 23:21
  • Actually now that I think about it, I put the print after the socket. I’m out right now, but when I get back in gonna try to put the print command first. I wonder if my program is getting hung up on my socket for some reason – gman1230321 May 20 '18 at 23:23
  • if I were you, I would try printing port before casting it to see if it has an integer value or not. – reza karimi May 20 '18 at 23:23
  • I’ll try but I know for certain that it doesn’t have anything because at that point the entry isn’t even created yet – gman1230321 May 20 '18 at 23:44
  • Well that's the problem. You can't convert a variable that is empty to int. what exactly is your question right now? – reza karimi May 20 '18 at 23:47
  • How can I make it wait to cast it until it has a value – gman1230321 May 20 '18 at 23:47
  • Also it’s a function so it’s not actually being casted yet – gman1230321 May 20 '18 at 23:48
  • Thats a very general question. You have to know what you want your application to do. If you expect the user to put in the right entery and then click the button, your code will work just fine. But if you want to continously check for the input, you have to use another thread to do so. – reza karimi May 20 '18 at 23:52
  • Well actually I miss worded my response there. Sorry. Really, the function doesn’t get called until a button is pressed. – gman1230321 May 20 '18 at 23:53
  • So giving the right entry will work fine – reza karimi May 21 '18 at 00:00
  • Yes, except, when the program gets compiled, the entry has nothing in it, hence it can’t parse. I need to get around that somehow – gman1230321 May 21 '18 at 00:17
  • So I’m very confused. I’m not getting any more errors but what I did was add a print to the begging of the try. I ran it and it printed out both prints. When I pressed the button(with a valid entry) I got no out put at all – gman1230321 May 21 '18 at 00:37
  • print at the very last line of the try section. also these comments are getting really long. you've got the answer to your original question, so probably asking another question will help you with that – reza karimi May 21 '18 at 00:39
  • Alright I’ll try that I’m also gonna try and search for some more general things on google that could get me some more answers – gman1230321 May 21 '18 at 00:41