python gui programming(newbie)

Cant get the program to run correctly

Page 1 of 1

5 Replies - 2144 Views - Last Post: 02 October 2010 - 10:04 PM Rate Topic: -----

#1 GotYahweh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 09

python gui programming(newbie)

Posted 02 October 2010 - 06:42 PM

I have a python port scanner program that I am trying to write a gui interface for it. Having an issue. The first issue was that I could not get the scan button to do anything, but that I think I got solved. I am looking for help and direction, Im new to python and to programming and I dont understand pretty much anything. So please if you say stuff like (Class, Command,) and so on please explain your self so I can understand too. Otherwise I will be saying whats that alot and ticking off lots of people. And this code may be completely wrong. Well here is my code:
------------------------------------------------------------
from socket import *

import Tkinter


from Tkinter import *

target = raw_input('Enter host to scan: ')  
targetIP = target
if __name__ == '__main__':  
           
     print 'Starting scan on host ', target
     

def call():


     for i in range(20, 1025):  
         s = socket(AF_INET, SOCK_STREAM)  
   
         result = s.connect_ex((targetIP, i))  
  
         if(result == 0) :  
             print 'Port %d: OPEN' % (i,)
         s.close()
         #if(result = 0) :
         #   print 'No ports open on host'
         # s.close()



root =Tk()
root.title('Port Scanner')
Label (text='Enter the host address to scan.').pack(side=TOP,padx=10,pady=10)
Entry(root, width=10).pack(side=TOP,padx=10,pady=10)
Button(root, text='Exit',command= root.quit).pack(side= RIGHT)
Button(root, text='Scan', command=call).pack(side= RIGHT)



Edited by Dogstopper: :code:

Especially in Python where indentation is crucial.

This post has been edited by Dogstopper: 02 October 2010 - 09:45 PM


Is This A Good Question/Topic? 0
  • +

Replies To: python gui programming(newbie)

#2 Brewer  Icon User is offline

  • Awesome
  • member icon

Reputation: 179
  • View blog
  • Posts: 1,044
  • Joined: 14-June 10

Re: python gui programming(newbie)

Posted 02 October 2010 - 06:49 PM

First off, while here on D.I.C, please be kind enough to wrap your code in [code] tags.

Secondly, if you can write a program like this, you are doing fine. I just started trying to learn Python and I can't understand what is going on in your program.

This post has been edited by .TheEngineer: 02 October 2010 - 06:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 GotYahweh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 11-March 09

Re: python gui programming(newbie)

Posted 02 October 2010 - 07:12 PM

I dont know what code tags are but I will try..
Was This Post Helpful? 0
  • +
  • -

#4 blutrane  Icon User is offline

  • 私もクールです

Reputation: 25
  • View blog
  • Posts: 1,934
  • Joined: 17-May 01

Re: python gui programming(newbie)

Posted 02 October 2010 - 09:31 PM

I take it this is 2.7? are any errors printing?

Also place the body of the code between
[code][/code]

It is especially important here because indenting is super important in python, its the way a block of code is differentiated, no brackets or anything just tab indents

This post has been edited by blutrane: 02 October 2010 - 09:35 PM

Was This Post Helpful? 0
  • +
  • -

#5 Eric115  Icon User is offline

  • coderさん
  • member icon

Reputation: 63
  • View blog
  • Posts: 696
  • Joined: 19-January 09

Re: python gui programming(newbie)

Posted 02 October 2010 - 09:34 PM

Ok, a good way to do a GUI is depending on how complex you want your GUI to be is just to use easygui. It does all of the basic input/output user selection stuff. If you want something that looks a little better and more customized but is still easy to use, you might want to look at PythonCard, drag and drop GUI making which can be edited from your code and all of the commands are easy to do e.g. You have a button called button1, when someone clicks it you want to print something, this is how it would look:
class(model.Background):
    def on_button1_mouseClick(self, event):
        print 'Something'


Its very good to use and is quite easy. I don't know much about tinker so I hope I could be some help.

Edit:
I think easygui is available for all python versions, but I am not sure in PythonCard goes past 2.5.x. You might need to check before you use it if you are using anything higher.

This post has been edited by Eric115: 02 October 2010 - 09:35 PM

Was This Post Helpful? 0
  • +
  • -

#6 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: python gui programming(newbie)

Posted 02 October 2010 - 10:04 PM

Alright, in general, you need to clean up this code a little bit. You have imports that you need to clean up.

calling root.mainloop() is the method call you need to start the GUI. However, you need your call method to have the targetIP variable set, so you need a way to access the data in the Entry. So you need to set up the Entry widget and assign it to a variable.

The way that you way instantiated the widgets and then called pack() on them in one fell swoop is not recommended. Generally, the more accepted method is to assign each widget to a variable first in case you ever need to access the widgets. The Entry widget has a get() method which gets all the text in there.

Here is how I implemented your changes:

from socket import *
from Tkinter import *

def call():
    targetIP = field.get()
    for i in range(20, 1025):  
         s = socket(AF_INET, SOCK_STREAM)  
   
         result = s.connect_ex((targetIP, i))  
  
         if(result == 0) :  
             print 'Port %d: OPEN' % (i,)
         s.close()
         #if(result = 0) :
         #   print 'No ports open on host'
         # s.close()

root = Tk()
root.title('Port Scanner')

lab = Label (text='Enter the host address to scan.')
lab.pack(side=TOP,padx=10,pady=10)

field = Entry(root, width=10)
field.pack(side=TOP,padx=10,pady=10)

Button(root, text='Exit',command= root.quit).pack(side= RIGHT)
Button(root, text='Scan', command=call).pack(side= RIGHT)

root.mainloop()



Once you become accustomed to Object Oriented Programming, I recommend that you make all your GUIs OO, because it makes it simpler to handle and organize.

I have a 5 part Tkinter tutorial series if you are interested:
http://www.dreaminco...dosmember=97990

And also a Notepad snippet if you want to look at it:
http://www.dreaminco...snippet4227.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1