|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | + |
| 4 | +#**************** Modules Require *****************# |
| 5 | +from tkinter import * |
| 6 | +import socket |
| 7 | +from urllib.request import urlopen |
| 8 | + |
| 9 | +#**************** Get IP commands *****************# |
| 10 | +#control buttons |
| 11 | +def get_wan_ip(): |
| 12 | + try : |
| 13 | + #get ip from http://ipecho.net/plain as text |
| 14 | + wan_ip=urlopen('http://ipecho.net/plain').read().decode('utf-8') |
| 15 | + res.configure(text='Wan IP is : '+wan_ip,fg='#600') |
| 16 | + except : |
| 17 | + res.configure(text='Problem in source : http://ipecho.net/plain',fg='red') |
| 18 | +#get local ip |
| 19 | +def get_local_ip(): |
| 20 | + try: |
| 21 | + lan_ip=(socket.gethostbyname(socket.gethostname())) |
| 22 | + res.configure(text='Local IP is : '+lan_ip,fg='#600') |
| 23 | + except: |
| 24 | + res.configure(text='Unkown Error',fg='#red') |
| 25 | +#**************** about control button *****************# |
| 26 | +#show about info and change the button command and place |
| 27 | +def about(): |
| 28 | + global close_app,frame,info |
| 29 | + about_app.destroy() |
| 30 | + frame=Frame(root,width=350,height=2,bg='blue') |
| 31 | + frame.grid(row=2,column=0,columnspan=4) |
| 32 | + info=Label(root,text=""" |
| 33 | + Practice Python |
| 34 | + Take idea from here : |
| 35 | + https://github.com/geekcomputers/Python/blob/master/myip.py |
| 36 | + """,fg='#02F') |
| 37 | + info.grid(row=3,column=0,columnspan=4,padx=5) |
| 38 | + close_app=Button(root,text='Close',command=close_about,bg='#55F') |
| 39 | + close_app.grid(row=4,column=0,columnspan=4,pady=5) |
| 40 | +#remove about info and remove close button then return about button in orignal place |
| 41 | +def close_about(): |
| 42 | + global frame,about_app,info |
| 43 | + info.destroy() |
| 44 | + frame.destroy() |
| 45 | + close_app.destroy() |
| 46 | + about_app=Button(root,text='about',command=about) |
| 47 | + about_app.grid(row=1,column=2,padx=5,pady=5,sticky=W) |
| 48 | + |
| 49 | +#**************** Tkinter GUI *****************# |
| 50 | +root=Tk() |
| 51 | +root.title ('Khaled programing practice') |
| 52 | +#all buttons |
| 53 | +res=Label(root,text='00.00.00.00',font=25) |
| 54 | +res_wan_ip=Button(root,text='Get Wan IP',command=get_wan_ip) |
| 55 | +res_local_ip=Button(root,text='Get Local IP',command=get_local_ip) |
| 56 | +about_app=Button(root,text='about',command=about) |
| 57 | +quit_app=Button(root,text='quit',command=quit,bg='#f40') |
| 58 | +#method grid to install the button in window |
| 59 | +res.grid(row=0,column=0,columnspan=4,sticky=N,padx=10,pady=5) |
| 60 | +res_wan_ip.grid(row=1,column=0,padx=5,pady=5,sticky=W) |
| 61 | +res_local_ip.grid(row=1,column=1,padx=5,pady=5,sticky=W) |
| 62 | +about_app.grid(row=1,column=2,padx=5,pady=5,sticky=W) |
| 63 | +quit_app.grid(row=1,column=3,padx=5,pady=5,sticky=E) |
| 64 | +#run GUI/app |
| 65 | +root.mainloop() |
| 66 | + |
| 67 | + |
| 68 | + |
0 commit comments