Python TK 实现ping (二)

来源:互联网 发布:华科软件学院 编辑:程序博客网 时间:2024/05/21 09:01


简单实现3类网段的Ping,使用os的popen,返回结果为CMD实际输出


from tkinter import *

import os

# 控件布局
root = Tk()

top_frame = Frame(root)
botton_frame = Frame(root)
text_frame = Frame(root)

#L1 = Label(top_frame,text=' IP 地址 ')
#L1.pack(side='left')

#E1 = Entry(top_frame,textvariable=default_value,bd=2)
#E1.pack(side=LEFT)

def btn_click():
    evalue = Ping()

def Ping():
    #获取文本框里面的数值
    startip = En1.get().split(".")
    endip = En2.get().split(".")

   
    ip3 = ".".join(startip[:3])
    ip1 = int(startip[-1])
    #print(ip1)
    while 0<ip1<255:
        
        result=os.popen("ping "+ip3+"."+str(ip1)).read()
        text.insert(END,result)
        text.update()
        ip1 += 1
        #print(ip1)
        if ip1 > int(endip[-1]):
            break

#B1 = Button(top_frame,text = '确定',command=btn_click)
#B1.pack(side=RIGHT)

botton_left = Frame(botton_frame)
botton_right = Frame(botton_frame)

label1 = Label(botton_left,text="%-5s" % "开始IP:")
label2 = Label(botton_left,text="%-5s" % "结束IP:")
label1.pack(side = 'top')
label2.pack(side = 'top')

En1 = Entry(botton_right)
En2 = Entry(botton_right)
En1.pack(side='top')
En2.pack(side='top')

B2 = Button(botton_frame,text='确定',command=btn_click)
B2.pack(side=RIGHT)

text = Text(text_frame,width = 30, height = 30)
text.pack()


#top_frame.pack()
botton_frame.pack()
botton_left.pack(side='left')
botton_right.pack(side='right')
text_frame.pack()
root.mainloop()

0 0