2016-05-20 86 views
1

我用tkinter製作GUI,允許我點擊一個按鈕來運行端口掃描。我有一個端口掃描腳本可以正常工作,我已經設法通過GUI上的按鈕打開端口掃描器,但是當我單獨運行端口掃描器時,我收到了一個我沒有收到的錯誤。Gui用按鈕打開端口掃描器

Exception in Tkinter callback 
Traceback (most recent call last): 
    File "C:\Users\Steve\AppData\Local\Programs\Python\Python35-32\lib\tkinter\__init__.py", line 1550, in __call__ 
    return self.func(*args) 
    File "<string>", line 51, in Scan 
NameError: name 'IP_Input' is not defined 

我的代碼:

class CallWrapper: 
    """Internal class. Stores function to call when some user 
    defined Tcl function is called e.g. after an event occurred.""" 
    def __init__(self, func, subst, widget): 
     """Store FUNC, SUBST and WIDGET as members.""" 
     self.func = func 
     self.subst = subst 
     self.widget = widget 

    def __call__(self, *args): 
     """Apply first function SUBST to arguments, than FUNC.""" 
     try: 
      if self.subst: 
       args = self.subst(*args) 
      return self.func(*args)   # THIS IS THE ERROR # 
     except SystemExit: 
      raise 
     except: 
      self.widget._report_exception() 


class XView: 
    """Mix-in class for querying and changing the horizontal position 
    of a widget's window.""" 

    def xview(self, *args): 
     """Query and change the horizontal position of the view.""" 
     res = self.tk.call(self._w, 'xview', *args) 

這是代碼以下爲第51行錯誤

def Scan(): 
    print ('Scan Called.') #Debugging 
    IP = str(IP_Input.get(0.0, tkinter.END)) #THIS IS ERROR LINE 51# 
    print ("IP #Debugging") 
    Start = int(PortS.get(0.0, tkinter.END)) 
    End = int(PortE.get(0.0, tkinter.END)) 
    TestSocket = socket.socket() 
    CurrentPort = Start 
    OpenPorts = 0 
    print ('Starting scan...') 
    HowFar = int(CurrentPort/End * 100) 
    ProgText = HowFar, r'%' 
    Label1.config(text=('Percentage Done:', ProgText)) 
+0

喜,歡呼聲,我希望這會有所幫助: – lee

+0

IP_Input = tkinter.Text(WIN) IP_Input.pack(擴大= tkinter.YES,填寫= tkinter.NONE) IP_Input.place_configure(寬度= 120,高度= 20) IP_Input.place_configure(X = 40,Y = 10) 打印( 「IP_Input完成」) – lee

+0

這是我的基本GUI - http://pastebin.com/1qgWQ4EL ,這是端口掃描程序 - http://pastebin.com/df2QQr8A,但問題似乎與這_init_.py文件http://pastebin.com/j03AxHPN – lee

回答

1

問題是與你的exec聲明。您正在打開名爲port_scanner.py的其他.py文件,然後調用exec(open("./port scanner.py))

這只是不會工作。

爲什麼這不起作用:

當你exec(open("path to .py file").read()) EXEC當然這段代碼執行的,但問題是,在這個文件中的全局變量不在範圍之內。

因此,爲了使這項工作(我不推薦),你就必須使用:

exec(open(path).read(), globals()) 

documentation

如果全局字典中不包含的值對於密鑰builtins,對內置模塊內置字典的引用插入該密鑰下。通過這種方式,您可以在將它傳遞給exec()之前,將自己的builtins字典插入到全局變量中,從而控制可執行代碼可用的內建函數。

如果你真的想用這種方式打電話給你的文件,那麼你應該只使用os.system

替代做法:

你真的不需要打電話給你的文件這樣。您現在有兩個Tk()運行實例。如果您需要另一個窗口,則爲此提供了一個小部件。它是Toplevel小部件。您可以重新構建代碼,以便在按鈕單擊時創建包含端口掃描器應用程序的實例Toplevel。例如,使用Toplevel小部件創建您的端口掃描器應用程序(如果您願意,可以在您的其他文件中),然後將「app」導入到您的文件中,然後點擊按鈕,使其初始化應用程序。

其他注意事項:

你調用一個while循環,如果運行(任何時間顯着的量),那麼這將阻止GUI的主事件循環,並導致您的GUI「掛」。

你的第一個猜想不應該是廣泛測試和使用的部分 python標準庫有缺陷。問題是(時99.9%)

while True: 
    print("In your own code.") 
+0

非常感謝你,我發現這非常有用,我很好奇,爲什麼不這樣做,exec(open(path).read(),globals()) – lee