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