Skip to content

Commit dd51c95

Browse files
Merge pull request #3042 from LessRinn/fixdocstring
changed file other_pepole/get_ip_gui
2 parents aace3f8 + f01da45 commit dd51c95

File tree

1 file changed

+70
-58
lines changed

1 file changed

+70
-58
lines changed

other_pepole/get_ip_gui

Lines changed: 70 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -2,72 +2,84 @@
22
# -*- coding: utf-8 -*-
33

44
import socket
5-
# **************** Modules Require *****************#
6-
from tkinter import *
5+
from tkinter import Tk, Label, Button, Frame
76
from urllib.request import urlopen
7+
from urllib.error import URLError
88

99

10-
# **************** Get IP commands *****************#
11-
# control buttons
12-
def get_wan_ip():
13-
try:
14-
# get ip from http://ipecho.net/plain as text
15-
wan_ip = urlopen('http://ipecho.net/plain').read().decode('utf-8')
16-
res.configure(text='Wan IP is : ' + wan_ip, fg='#600')
17-
except:
18-
res.configure(text='Problem in source : http://ipecho.net/plain', fg='red')
10+
class IPApp:
11+
'''A simple GUI application to get WAN and local IP addresses.'''
12+
def __init__(self):
13+
'''Initialize the application'''
14+
self.root = Tk()
15+
self.root.title('Khaled programming practice')
16+
self._setup_ui()
1917

18+
def _setup_ui(self) -> None:
19+
"""Initialize the user interface"""
20+
# Result label
21+
self.res = Label(self.root, text='00.00.00.00', font=25)
22+
self.res.grid(row=0, column=0, columnspan=4, sticky='N', padx=10, pady=5)
2023

21-
# get local ip
22-
def get_local_ip():
23-
try:
24-
lan_ip = (socket.gethostbyname(socket.gethostname()))
25-
res.configure(text='Local IP is : ' + lan_ip, fg='#600')
26-
except:
27-
res.configure(text='Unkown Error', fg='#red')
28-
# **************** about control button *****************#
24+
# Buttons
25+
Button(self.root, text='Get Wan IP', command=self.get_wan_ip).grid(
26+
row=1, column=0, padx=5, pady=5, sticky='W')
27+
Button(self.root, text='Get Local IP', command=self.get_local_ip).grid(
28+
row=1, column=1, padx=5, pady=5, sticky='W')
29+
Button(self.root, text='About', command=self.show_about).grid(
30+
row=1, column=2, padx=5, pady=5, sticky='W')
31+
Button(self.root, text='Quit', command=self.root.quit, bg='#f40').grid(
32+
row=1, column=3, padx=5, pady=5, sticky='E')
2933

34+
# About section widgets (initially hidden)
35+
self.about_frame = Frame(self.root, width=350, height=2, bg='blue')
36+
self.about_info = Label(self.root, text="""\
37+
Practice Python
38+
Take idea from here:
39+
https://github.com/geekcomputers/Python/blob/master/myip.py
40+
""", fg='#02F')
41+
self.about_close = Button(self.root, text='Close',
42+
command=self.hide_about, bg='#55F')
3043

31-
# show about info and change the button command and place
32-
def about():
33-
global close_app, frame, info
34-
about_app.destroy()
35-
frame = Frame(root, width=350, height=2, bg='blue')
36-
frame.grid(row=2, column=0, columnspan=4)
37-
info = Label(root, text="""
38-
Practice Python
39-
Take idea from here :
40-
https://github.com/geekcomputers/Python/blob/master/myip.py
41-
""", fg='#02F')
42-
info.grid(row=3, column=0, columnspan=4, padx=5)
43-
close_app = Button(root, text='Close', command=close_about, bg='#55F')
44-
close_app.grid(row=4, column=0, columnspan=4, pady=5)
44+
def get_wan_ip(self) -> None:
45+
"""Get and display WAN IP address"""
46+
try:
47+
wan_ip = urlopen('http://ipecho.net/plain', timeout=5).read().decode('utf-8')
48+
self.res.configure(text=f'WAN IP is: {wan_ip}', fg='#600')
49+
except URLError as e:
50+
self.res.configure(text=f'Network error: {e.reason}', fg='red')
51+
except Exception as e:
52+
self.res.configure(text=f'Unexpected error: {str(e)}', fg='red')
4553

54+
def get_local_ip(self) -> None:
55+
"""Get and display local IP address"""
56+
try:
57+
local_ip = socket.gethostbyname(socket.gethostname())
58+
self.res.configure(text=f'Local IP is: {local_ip}', fg='#600')
59+
except Exception as e:
60+
self.res.configure(text=f'Error getting local IP: {str(e)}', fg='red')
4661

47-
# remove about info and remove close button then return about button in orignal place
48-
def close_about():
49-
global frame, about_app, info
50-
info.destroy()
51-
frame.destroy()
52-
close_app.destroy()
53-
about_app = Button(root, text='about', command=about)
54-
about_app.grid(row=1, column=2, padx=5, pady=5, sticky=W)
62+
def show_about(self) -> None:
63+
"""Show about information"""
64+
self.about_frame.grid(row=2, column=0, columnspan=4)
65+
self.about_info.grid(row=3, column=0, columnspan=4, padx=5)
66+
self.about_close.grid(row=4, column=0, columnspan=4, pady=5)
5567

68+
def hide_about(self) -> None:
69+
"""Hide about information"""
70+
self.about_frame.grid_remove()
71+
self.about_info.grid_remove()
72+
self.about_close.grid_remove()
5673

57-
# **************** Tkinter GUI *****************#
58-
root = Tk()
59-
root.title('Khaled programing practice')
60-
# all buttons
61-
res = Label(root, text='00.00.00.00', font=25)
62-
res_wan_ip = Button(root, text='Get Wan IP', command=get_wan_ip)
63-
res_local_ip = Button(root, text='Get Local IP', command=get_local_ip)
64-
about_app = Button(root, text='about', command=about)
65-
quit_app = Button(root, text='quit', command=quit, bg='#f40')
66-
# method grid to install the button in window
67-
res.grid(row=0, column=0, columnspan=4, sticky=N, padx=10, pady=5)
68-
res_wan_ip.grid(row=1, column=0, padx=5, pady=5, sticky=W)
69-
res_local_ip.grid(row=1, column=1, padx=5, pady=5, sticky=W)
70-
about_app.grid(row=1, column=2, padx=5, pady=5, sticky=W)
71-
quit_app.grid(row=1, column=3, padx=5, pady=5, sticky=E)
72-
# run GUI/app
73-
root.mainloop()
74+
def run(self) -> None:
75+
"""Start the application"""
76+
self.root.mainloop()
77+
78+
79+
def main() -> None:
80+
app = IPApp()
81+
app.run()
82+
83+
84+
if __name__ == '__main__':
85+
main()

0 commit comments

Comments
 (0)