Skip to content

Commit f01da45

Browse files
committed
chenged to OOP style
1 parent e3a9459 commit f01da45

File tree

1 file changed

+68
-62
lines changed

1 file changed

+68
-62
lines changed

other_pepole/get_ip_gui

Lines changed: 68 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -2,78 +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
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()
1217

13-
def get_wan_ip() -> None:
14-
'''get wan ip'''
15-
try:
16-
# get ip from http://ipecho.net/plain as text
17-
wan_ip = urlopen('http://ipecho.net/plain').read().decode('utf-8')
18-
res.configure(text='Wan IP is : ' + wan_ip, fg='#600')
19-
except:
20-
res.configure(text='Problem in source : http://ipecho.net/plain', fg='red')
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)
2123

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')
2233

23-
def get_local_ip() -> None:
24-
'''get local ip'''
25-
try:
26-
lan_ip = (socket.gethostbyname(socket.gethostname()))
27-
res.configure(text='Local IP is : ' + lan_ip, fg='#600')
28-
except:
29-
res.configure(text='Unkown Error', fg='#red')
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

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')
3153

32-
def about():
33-
'''show about info and change the button command and place'''
34-
global close_app, frame, info
35-
about_app.destroy()
36-
frame = Frame(root, width=350, height=2, bg='blue')
37-
frame.grid(row=2, column=0, columnspan=4)
38-
info = Label(root, text="""
39-
Practice Python
40-
Take idea from here :
41-
https://github.com/geekcomputers/Python/blob/master/myip.py
42-
""", fg='#02F')
43-
info.grid(row=3, column=0, columnspan=4, padx=5)
44-
close_app = Button(root, text='Close', command=close_about, bg='#55F')
45-
close_app.grid(row=4, column=0, columnspan=4, pady=5)
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

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)
4767

48-
def close_about() -> None:
49-
'''remove about info and remove close button then return about button in orignal place'''
50-
global frame, about_app, info
51-
info.destroy()
52-
frame.destroy()
53-
close_app.destroy()
54-
about_app = Button(root, text='about', command=about)
55-
about_app.grid(row=1, column=2, padx=5, pady=5, sticky=W)
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()
73+
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()
5682

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

7884
if __name__ == '__main__':
79-
start_app()
85+
main()

0 commit comments

Comments
 (0)