The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Adding code to GUI
Discuss Adding code to GUI in the Python Programming forum on Dev Shed. Adding code to GUI Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 30th, 2003, 12:21 AM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: Somewhere over the Rainbow
Posts: 128
Time spent in forums: 3 h 54 m 28 sec
Reputation Power: 10
|
|
|
Adding code to GUI
Hi, I would like to know why doesnt works my below code?, dont show any error message! Thanks.
Code:
def port(self):
try:
t = tkSimpleDialog.askstring('DATA',
ENTER IP or HOST:',
parent=self.TextBox)
host = t[1]
maxport = int(t[2]) + 1
for port in xrange(1, maxport):
s = socket(AF_INET, SOCK_STREAM)
try:
s.connect((host, port))
self.TextBox.insert(Tkinter.END,"Port %d is open" % port)
except error:
continue
except IndexError:
self.TextBox.insert(Tkinter.END, "Usage: %s <ip address> <max port>" % t[0])
except ValueError:
self.TextBox.insert(Tkinter.END, "Port must be a valid integer")
except Exception, e:
print e
|

November 30th, 2003, 06:55 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
I'd love to tell you but it seems like we've only got a small bit of your actual program here, could you attach the whole scrript?
The reason your not getting an error message would be because you wrapped your code up inside try-except blocks  which are kinda like walls errors can't pass though.
Mark.
__________________
programming language development: www.netytan.com – Hula
|

November 30th, 2003, 12:34 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: Somewhere over the Rainbow
Posts: 128
Time spent in forums: 3 h 54 m 28 sec
Reputation Power: 10
|
|
Thanks for your reply, here is my whole script
Code:
import Tkinter
import tkFileDialog
import tkSimpleDialog
import string
from socket import *
TEXT_FILE_TYPES=[("PRO Scanner Files","scn"),("All files","*")]
class Scanner:
def __init__(self):
self.FileName=None
self.CreateWidgets()
def CreateWidgets(self):
self.root=Tkinter.Tk()
self.root.title("New file")
MainFrame=Tkinter.Frame(self.root)
MenuFrame=Tkinter.Frame(self.root)
MenuFrame.pack(side=Tkinter.TOP,fill=Tkinter.X)
FileMenuButton=Tkinter.Menubutton(MenuFrame,
text="File",underline=0)
FileMenuButton.pack(side=Tkinter.LEFT,anchor=Tkinter.W)
FileMenu=Tkinter.Menu(FileMenuButton,tearoff=0)
FileMenu.add_command(label="New",underline=0,
command=self.port)
FileMenuButton["menu"]=FileMenu
# Create the main text field:
self.TextBox=Tkinter.Text(MainFrame,background="white",foreground="black")
self.TextBox.pack(fill=Tkinter.BOTH,expand=Tkinter.YES)
# Pack the top-level widget:
MainFrame.pack(fill=Tkinter.BOTH,expand=Tkinter.YES)
def port(self):
try:
t = tkSimpleDialog.askstring('INGRESO DE DATOS',
'Ingrese la IP o HOST:',
parent=self.TextBox)
host = t[1]
maxport = int(t[2]) + 1
for port in xrange(1, maxport):
s = socket(AF_INET, SOCK_STREAM)
try:
s.connect((host, port))
self.TextBox.insert(Tkinter.END,"Port %d is open" % port)
except error:
continue
except IndexError:
self.TextBox.insert(Tkinter.END, "Usage: %s <ip address> <max port>" % t[0])
except ValueError:
self.TextBox.insert(Tkinter.END, "Port must be a valid integer")
except Exception, e:
print e
def Run(self):
self.root.mainloop()
if (__name__=="__main__"):
Scanner().Run()
|

November 30th, 2003, 02:44 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
When i run your script it works fine, at least up untill i enter an IP (my localhost) then it freezes.. so i think we can narror it down too somewhere around your s.connect() line.
Unfortunatly im not really a Tk expert; too be sure i'd try removing the socket and see if it works then, at least then we know where to start!
Mark.
|

November 30th, 2003, 03:36 PM
|
 |
Mini me.
|
|
Join Date: Nov 2003
Location: Cambridge, UK
|
|
|
In port() I guess you need to split the response sting t first and index from 0 not one.
You could consider a regular expression check to confirm the valid format of the user input.
|

November 30th, 2003, 03:45 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
|
Oh, while i think of it, you should move 's = socket(AF_INET, SOCK_STREAM)' to outside your loop! you def' don't want to create a new socket instance each time!
mark.
|

November 30th, 2003, 08:57 PM
|
 |
Contributing User
|
|
Join Date: Aug 2003
Location: Somewhere over the Rainbow
Posts: 128
Time spent in forums: 3 h 54 m 28 sec
Reputation Power: 10
|
|
I changed it without Tkinter, and now is working fine. How can I add the results into the Textbox in the Tkinter code?
Thanks a lot.
Code:
#!/usr/bin/env python
from socket import *
import sys
try:
host = sys.argv[1]
maxport = int(sys.argv[2]) + 1
for port in xrange(1, maxport):
s = socket(AF_INET, SOCK_STREAM)
try:
s.connect((host, port))
print "Port %d is open" % port
except error:
continue
except IndexError:
print "Usage: %s <ip address> <max port>" % sys.argv[0]
except ValueError:
print "Port must be a valid integer"
except Exception, e:
print e
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|