From 1d72a70217a0d74db4b27bbe689f4b34ad063c47 Mon Sep 17 00:00:00 2001 From: Less Rinn Date: Sat, 8 Nov 2025 12:50:51 +0300 Subject: [PATCH 1/3] fix docstrings and add start_app function in other_pepole/get_ip_gui --- other_pepole/get_ip_gui | 55 +++++++++++++++++++++++------------------ 1 file changed, 31 insertions(+), 24 deletions(-) diff --git a/other_pepole/get_ip_gui b/other_pepole/get_ip_gui index 5728697ac5b..ba696b2eca2 100755 --- a/other_pepole/get_ip_gui +++ b/other_pepole/get_ip_gui @@ -1,6 +1,7 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- +from logging import root import socket # **************** Modules Require *****************# from tkinter import * @@ -9,7 +10,9 @@ from urllib.request import urlopen # **************** Get IP commands *****************# # control buttons -def get_wan_ip(): + +def get_wan_ip() -> None: + '''get wan ip''' try: # get ip from http://ipecho.net/plain as text wan_ip = urlopen('http://ipecho.net/plain').read().decode('utf-8') @@ -18,18 +21,17 @@ def get_wan_ip(): res.configure(text='Problem in source : http://ipecho.net/plain', fg='red') -# get local ip -def get_local_ip(): +def get_local_ip() -> None: + '''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(): + '''show about info and change the button command and place''' global close_app, frame, info about_app.destroy() frame = Frame(root, width=350, height=2, bg='blue') @@ -44,8 +46,8 @@ def about(): 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(): +def close_about() -> None: + '''remove about info and remove close button then return about button in orignal place''' global frame, about_app, info info.destroy() frame.destroy() @@ -53,21 +55,26 @@ def close_about(): about_app = Button(root, text='about', command=about) about_app.grid(row=1, column=2, padx=5, pady=5, sticky=W) +def start_app() -> None: + '''start the app''' + global root, res, about_app + # **************** 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() -# **************** 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() +if __name__ == '__main__': + start_app() \ No newline at end of file From e3a94592d17f5dbcb740cfaf3cbda510557cb87e Mon Sep 17 00:00:00 2001 From: Less Rinn Date: Sat, 8 Nov 2025 12:54:04 +0300 Subject: [PATCH 2/3] fix --- other_pepole/get_ip_gui | 1 - 1 file changed, 1 deletion(-) diff --git a/other_pepole/get_ip_gui b/other_pepole/get_ip_gui index ba696b2eca2..12aba5273e5 100755 --- a/other_pepole/get_ip_gui +++ b/other_pepole/get_ip_gui @@ -1,7 +1,6 @@ #!/usr/bin/env python3 # -*- coding: utf-8 -*- -from logging import root import socket # **************** Modules Require *****************# from tkinter import * From f01da45b1672247fbfaf8e39f4f52cf36775fcb9 Mon Sep 17 00:00:00 2001 From: Less Rinn Date: Sat, 8 Nov 2025 13:11:31 +0300 Subject: [PATCH 3/3] chenged to OOP style --- other_pepole/get_ip_gui | 130 +++++++++++++++++++++------------------- 1 file changed, 68 insertions(+), 62 deletions(-) diff --git a/other_pepole/get_ip_gui b/other_pepole/get_ip_gui index 12aba5273e5..8043831c3bd 100755 --- a/other_pepole/get_ip_gui +++ b/other_pepole/get_ip_gui @@ -2,78 +2,84 @@ # -*- coding: utf-8 -*- import socket -# **************** Modules Require *****************# -from tkinter import * +from tkinter import Tk, Label, Button, Frame from urllib.request import urlopen +from urllib.error import URLError -# **************** Get IP commands *****************# -# control buttons +class IPApp: + '''A simple GUI application to get WAN and local IP addresses.''' + def __init__(self): + '''Initialize the application''' + self.root = Tk() + self.root.title('Khaled programming practice') + self._setup_ui() -def get_wan_ip() -> None: - '''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') + def _setup_ui(self) -> None: + """Initialize the user interface""" + # Result label + self.res = Label(self.root, text='00.00.00.00', font=25) + self.res.grid(row=0, column=0, columnspan=4, sticky='N', padx=10, pady=5) + # Buttons + Button(self.root, text='Get Wan IP', command=self.get_wan_ip).grid( + row=1, column=0, padx=5, pady=5, sticky='W') + Button(self.root, text='Get Local IP', command=self.get_local_ip).grid( + row=1, column=1, padx=5, pady=5, sticky='W') + Button(self.root, text='About', command=self.show_about).grid( + row=1, column=2, padx=5, pady=5, sticky='W') + Button(self.root, text='Quit', command=self.root.quit, bg='#f40').grid( + row=1, column=3, padx=5, pady=5, sticky='E') -def get_local_ip() -> None: - '''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 section widgets (initially hidden) + self.about_frame = Frame(self.root, width=350, height=2, bg='blue') + self.about_info = Label(self.root, text="""\ +Practice Python +Take idea from here: +https://github.com/geekcomputers/Python/blob/master/myip.py +""", fg='#02F') + self.about_close = Button(self.root, text='Close', + command=self.hide_about, bg='#55F') + def get_wan_ip(self) -> None: + """Get and display WAN IP address""" + try: + wan_ip = urlopen('http://ipecho.net/plain', timeout=5).read().decode('utf-8') + self.res.configure(text=f'WAN IP is: {wan_ip}', fg='#600') + except URLError as e: + self.res.configure(text=f'Network error: {e.reason}', fg='red') + except Exception as e: + self.res.configure(text=f'Unexpected error: {str(e)}', fg='red') -def about(): - '''show about info and change the button command and place''' - 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) + def get_local_ip(self) -> None: + """Get and display local IP address""" + try: + local_ip = socket.gethostbyname(socket.gethostname()) + self.res.configure(text=f'Local IP is: {local_ip}', fg='#600') + except Exception as e: + self.res.configure(text=f'Error getting local IP: {str(e)}', fg='red') + def show_about(self) -> None: + """Show about information""" + self.about_frame.grid(row=2, column=0, columnspan=4) + self.about_info.grid(row=3, column=0, columnspan=4, padx=5) + self.about_close.grid(row=4, column=0, columnspan=4, pady=5) -def close_about() -> None: - '''remove about info and remove close button then return about button in orignal place''' - 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) + def hide_about(self) -> None: + """Hide about information""" + self.about_frame.grid_remove() + self.about_info.grid_remove() + self.about_close.grid_remove() + + def run(self) -> None: + """Start the application""" + self.root.mainloop() + + +def main() -> None: + app = IPApp() + app.run() -def start_app() -> None: - '''start the app''' - global root, res, about_app - # **************** 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() if __name__ == '__main__': - start_app() \ No newline at end of file + main() \ No newline at end of file