Last active
October 3, 2021 23:39
-
-
Save cassiobotaro/73d887f2421dcf96bc879d808c87c149 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from threading import Thread | |
import queue | |
import requests | |
import tkinter as tk | |
from time import strftime | |
# class personalized to sent email to the manager team | |
# from teho_conn import * | |
q = queue.Queue() | |
def worker(): | |
while True: | |
assunto = q.get() | |
print(f'Mandando email com o assunto {assunto}') | |
q.task_done() | |
class IntrCnncCheck(tk.Tk): | |
""" | |
Check internet connection | |
Start showing traffic lights window | |
""" | |
def __init__(self, *args, **kwargs): | |
super().__init__(*args, **kwargs) | |
# super().__init__(self, *args, **kwargs) | |
self.container = tk.Frame(self) | |
# self.container.pack(side="top", fill="both", expand=True) | |
self.title("**Tech C Brasil**") | |
# define window dimensions width and height | |
self.wndw_wdth = 240 | |
self.wndw_hght = 80 | |
# get the screen size of your computer [width and height using the root object as foolows] | |
self.scrn_wdth = self.winfo_screenwidth() #1366 | |
self.scrn_hgth = self.winfo_screenheight() #768 | |
# get the window position from the top dynamically as well a position from left or right as follows | |
self.posy = int(self.scrn_hgth/2 - self.wndw_hght/2) | |
self.posx = int(self.scrn_wdth/2 - self.wndw_wdth/2) | |
# this is the line that will center your window | |
self.geometry(f'{self.wndw_wdth}x{self.wndw_hght}+{self.posx}+{self.posy}') | |
self.resizable(1, 1) | |
self.minsize(width=self.wndw_wdth,height=self.wndw_hght) | |
self.maxsize(width=340,height=120) | |
self.lght = tk.Canvas(self, width=40, height=58) | |
#self.lght.place(x=20,y=10,width=110,height=130) | |
self.lght.grid(row=0, column=0, padx=0, pady=0) | |
self.lght_oval = self.lght.create_oval(20, 20, 40, 40) | |
self.lght.itemconfig(self.lght_oval, fill='red') | |
self.labl_vrvl = tk.StringVar() | |
self.labl_vrvl.set('Internet Desconectada') | |
self.labl_01 = tk.Label(self, textvariable=self.labl_vrvl) | |
#self.labl_01.place(x=70,y=33,width=123,height=10) | |
self.labl_01.grid(row=0, column=1, padx=0, pady=0) # margins | |
self.is_connected = self.labl_vrvl.get() == 'Internet Conectada' | |
self.intrcnnc() | |
self.lght.after(500, self.intrcnnc) | |
def intrcnnc(self): | |
"""Check the internet connection and update the traffic lights""" | |
try: | |
response = requests.get('https://www.youtube.com') | |
response.raise_for_status() | |
except Exception as e: | |
if self.is_connected: | |
self.is_connected = False | |
q.put('Internet Desconectada') | |
self.lght.itemconfig(self.lght_oval, fill='red') | |
self.labl_vrvl.set('Internet Desconectada') | |
else: | |
self.lght.itemconfig(self.lght_oval, fill="green") | |
self.labl_vrvl.set('Internet Conectada') | |
self.is_connected = True | |
# app.update() | |
self.lght.update() | |
self.lght.after(2000, self.intrcnnc) | |
def on_closing(self): | |
""" | |
Close the window | |
""" | |
self.destroy() | |
self.quit() | |
if __name__ == "__main__": | |
intr_cnnc_check = IntrCnncCheck() | |
thread = Thread(target=worker, daemon=True) | |
thread.start() | |
intr_cnnc_check.protocol("WM_DELETE_WINDOW", intr_cnnc_check.on_closing) | |
intr_cnnc_check.mainloop() | |
q.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment