Mensagens

A mostrar mensagens de maio, 2022

Converter Imagens (usando Tkinter)

from tkinter import * from tkinter import filedialog from PIL import Image root =Tk() class imagens(): def __init__ ( self ): self .root = root self .janela() self .frac() root.mainloop() def janela ( self ): self .root.title( "Converter Imagem" ) self .root.geometry( "450x200" ) self .root.resizable( False, False ) def frac ( self ): self .texto1_lb = Label( text = "Converter Imagem" , font =( "Helvetica" , '15' )) self .texto1_lb.place( relx = 0.3 , rely = 0.05 ) # Butões # self .bt_png = Button( text = "Selecionar um ficheiro PNG " , bd = 2 , font =( 'verdana' , '8' , 'bold' ) , bg = 'gray18' , fg = 'white' , command = self .selecionarimagem) self .bt_png.place( relx = 0.25...

Jogo Pedra, Papel e Tesoura (usando Tkinter )

from tkinter import * import random root = Tk() class jogo_papel_pedra_tesoura(): def __init__ ( self ): self .root = root self .janela() self .app() # criando o Loop root.mainloop() def janela ( self ): self .root.title( "Jogo papel pedra tesoura" ) self .root.configure( background = 'grey' ) self .root.geometry( "500x450" ) self .root.resizable( False, False ) def app ( self ): self .texto1 = Label(root , text = "Pedra Papel Tesoura" , font = "normal 20 bold" , fg = "black" ) self .texto1.place( relx = 0.2 , rely = 0.05 ) self .texto2 = Label(root , text = "Jogador 1" , font = "normal 12 bold" , fg = "red" ) self .texto2.place( relx = 0.23 , rely = 0.2 ) self .texto3 = Label(root , text = "Computador" , ...

Gravar vídeo (open cv)

import cv2 nomeficheiro = str ( input ( "Nome do Ficheiro: " )) captura = cv2.VideoCapture( 0 ) texto= nomeficheiro + str ( '.mp4' ) salida = cv2.VideoWriter(texto , cv2.VideoWriter_fourcc(* 'XVID' ) , 20.0 , ( 640 , 480 )) while (captura.isOpened()): ret , imagen = captura.read() if ret == True : cv2.imshow( 'video' , imagen) salida.write(imagen) if cv2.waitKey( 1 ) & 0xFF == ord ( 'q' ): break else : break captura.release() salida.release() cv2.destroyAllWindows()

Procurar música (usando tkinter)

from tkinter import * from tkinter import messagebox import pywhatkit as kit root = Tk() root.title( "Ouvir música no Youtube" ) root.geometry( '400x200' ) root.resizable( False,False ) def Buttonclick (): n1 = texto.get() kit.playonyt(n1) messagebox.showinfo( 'Pesquisa de Música no Youtube ' , 'Aqui está a música procurada! ' ) l=Label(root , text = 'Qual é a música que está a procurar?' , font = "arial" ) l.place( relx = 0.1 , rely = 0.05 ) texto = StringVar() entrada =Entry( textvariable =texto) entrada.place( relx = 0.35 , rely = 0.35 ) botao_1 = Button(root , text = 'Tocar!' , background = 'white' , fg = 'red' , command =Buttonclick) botao_1.place( relx = 0.35 , rely = 0.55 , relwidth = 0.3 ) root.mainloop()

Cartão Amarelo no futebol

cartaoamarelo = 0 while cartaoamarelo <= 1 : mostrarcartao = str ( input ( "Quer mostrar o cartão amarelo [S/N]: " )).lower() if mostrarcartao== 's' : print ( "Mostrou cartão amarelo" ) cartaoamarelo = cartaoamarelo + 1 if cartaoamarelo == 2 : print ( "Mostrado o segundo cartão amarelo" ) print ( "Mostrar cartão vermelho" ) print ( "O Jogador está expulso" ) elif mostrarcartao== 'n' : if cartaoamarelo == 1 : print ( "O Jogador continua a ter um cartão amarelo" ) else : print ( f'O Jogador continua a ter { cartaoamarelo } cartões amarelos' ) else : print ( "Erro" )

Calendário de qualquer ano (usando Tkinter)

from tkinter import * import calendar def mostrarcalendario (): root2 = Tk() root2.config( background = 'white' ) root2.title( "Calendário" ) root2.geometry( "450x600" ) root2.resizable( False,False ) ano_calendario = ano.get() mostrar= calendar.calendar(ano_calendario) calano = Label(root2 , text = mostrar , font =( "times" , 10 , )) calano.place( relx = 0.05 , rely = 0.05 ) root2.mainloop() root = Tk() def sair (): root.destroy() root.title( "Calendário" ) root.geometry( "400x300" ) root.configure( background = "white" ) root.resizable( False,False ) texto1 = Label(root , text = 'Calendário de qualquer Ano' , font =( "Ariel" , "15" , 'bold' ) , fg = "black" , bg = "white" ) texto1.place( relx = 0.18 , rely = 0.05 ) texto2 = Label(root , text = 'Digite o Ano' , font =( "Ariel" , "12...

Medida de comprimento (usando Tkinter)

from tkinter import * root =Tk() class app(): def __init__ ( self ): self .root = root self .janela() self .converter() root.mainloop() def janela ( self ): self .root.title( "Converter Quilómetros " ) self .root.geometry( "350x250" ) self .root.resizable( False, False ) self .root.configure( background = '#eaeef5' ) def converter ( self ): # self .km = DoubleVar() self .km_lb = Label( text = " Coloque os Quilómetros: " , font =( "Helvetica" , '10' , 'bold' ) , bg = '#78c8c0' , fg = '#0a0fc9' ) self .km_lb.place( relx = 0.05 , rely = 0.1 ) self .km_entry = Entry( textvariable = self .km) self .km_entry.place( relx = 0.6 , rely = 0.1 , relwidth = 0.2 ) # Resultado self .resultadomile = StringVar() ...

Calcular a percentagem (simples / tkinter)

total = int ( input ( "Digite total da população: " )) parcial = int ( input ( "Digite o número do grupo que pertende calcular: " )) percentagem = (parcial/total) * 100 print ( f'A percentagem é de { percentagem } %' ) # Usando Tkinter from tkinter import * root = Tk() class calcularpercentagem(): def __init__ ( self ): self .root = root self .janela() self .percentagem() # criando o Loop root.mainloop() def janela ( self ): self .root.title( "Calcular percentagem" ) self .root.configure( background = '#B0C4DE' ) self .root.geometry( "350x250" ) self .root.resizable( False, False ) def percentagem ( self ): self .amostra = IntVar() self .label_amostra = Label( text = "Quantidade : " ) self .label_amostra.place( relx = 0.3 , rely = 0.1 ) self .enty_amostra = Entry( textvariable = self .amostra) self...

Pesquisa binária

def pesquisa (lista , chave): baixo = 0 alto = len (lista)- 1 encontrar = False while baixo<=alto and not encontrar: media =(baixo+alto)// 2 if chave == lista[media]: encontrar = True elif chave>lista[media]: baixo = media + 1 else : alto = media- 1 if encontrar == True : print ( "Chave foi encontrada" ) else : print ( "Chave não foi encontrada" ) lista = [ 23 , 1 , 4 , 2 , 3 ] lista.sort() print (lista) chave = int ( input ( 'Escreve a chave: ' )) pesquisa(lista , chave)

Terminal de Cartão (versão final)

import time op = True while op== True : montante = float ( input ( "Intruduza o montante: " )) print ( "Insira o Cartão" ) time.sleep( 3 ) codigo_valido = '1234' cont = 1 codigo_marcado = 0 while codigo_valido != codigo_marcado or cont > 3 : codigo_marcado = str ( input ( "Digite o código: " )) if codigo_valido != codigo_marcado: time.sleep( 2 ) print ( "Código Errado" ) cont = cont + 1 if cont == 4 : print ( "Cartão Retido" ) confirmar = str ( input ( f" \n Confirmar o montante de compra(carregue em c)" f" \n{ montante } euros \n " )).lower() if confirmar == 'c' : time.sleep( 1 ) print ( "Compra efectuada com Sucesso!" ) else : time.sleep( 1 ) print ( "Compra cancelada!" )

Fechar janela (usando Tkinter)

from tkinter import * def fechar (): root.destroy() root = Tk() root.geometry( '300x100' ) root.title( 'Fechar a janela' ) root.resizable( False,False ) butao = Button(root , text = 'Fechar a janela' , command =fechar) butao.place( relx = 0.35 , rely = 0.35 ) root.mainloop() # Exemplo 2 from tkinter import * from tkinter import messagebox as mb def funcaobutao (): pergunta=mb.askquestion( 'Sair da Aplicação' , 'Quer sair da Apliação' ) if pergunta == 'yes' : root.destroy() else : mb.showinfo( 'Informação' , 'Voltar à Aplicação' ) root = Tk() root.geometry( '300x100' ) root.title( 'Fechar a janela' ) root.resizable( False,False ) butao = Button(root , text = 'Fechar a janela' , command =funcaobutao) butao.place( relx = 0.35 , rely = 0.35 ) root.mainloop() # Exemplo 3 from tkinter import * from tkinter import messagebox as mb def funcaobutao (): mb.showinfo( ...

Fechar uma janela Tkinter de forma automática

from tkinter import * root = Tk() root.geometry( "750x270" ) texto = Label(root , text = "Esta janela será fechada após 3 segundos..." , font =( 'Helvetica 20 bold' )) texto.place( relx = 0.05 , rely = 0.05 ) #Fechar automaticamente a janela após 3 segundos root.after( 3000 ,lambda :root.destroy()) root.mainloop()

Tirar a Senha de Espera (usando tkinter )

from tkinter import * nuatgeral = 0 nuc = 0 # Funções def atendimentogeral (): root2 = Toplevel(root) root2.title( "Atendimento Geral" ) root2.geometry( "300x300" ) root2.resizable( False,False ) root2.configure( background = "black" ) text = Label(root2 , text = 'Atendimento Geral' , font =( "Ariel" , "15" , 'bold' ) , fg = "white" , bg = "black" ) text.place( relx = 0.2 , rely = 0.05 ) textClick = Label(root2 , text = 0 , font =( "Ariel" , "70" ) , fg = "red" , bg = "black" ) textClick.place( relx = 0.35 , rely = 0.2 ) global nuatgeral nuatgeral += 1 if nuatgeral == 100 : nuatgeral = 0 textClick.config( text =nuatgeral) # Fechar a Janela de Forma automática root2.after( 3000 , lambda : root2.destroy()) def cobranca (): root3 = Toplevel(root) root3.title( "Cobrança...

Placar de Chamada de pessoas

from tkinter import * counterCheck = 0 def checkClick (): global counterCheck counterCheck += 1 if counterCheck == 100 : counterCheck = 0 textClick.config( text =counterCheck) root = Tk() root.title( "Placar" ) root.geometry( "200x200" ) root.configure( background = "black" ) root.resizable( False,False ) textClick = Label(root , text = 0 , font =( "Ariel" , "70" ) , fg = "red" , bg = "black" ) textClick.place( relx = 0.2 , rely = 0.15 ) ClickCounter = Button(root , text = "SEGUINTE" , command =checkClick) ClickCounter.place( relx = 0.35 , rely = 0.8 ) ClickCounter.bind( "<space>" , checkClick) ClickCounter.focus_force() root.mainloop()

Terminal de Cartão

import time escolha1 = True while escolha1 == True : print ( 'Por favor, insire o cartão.' ) time.sleep( 2 ) codigo = str ( input ( 'Introduza o código: ' )) valido = '12345' if codigo != valido: print ( "Erro no código" ) else : print ( "Compra aceite!" ) def inicia (): montante = float ( input ( 'Introduz o montante: ' )) print ( "Por favor, insira o cartão." ) import time time.sleep( 2 ) cont = 1 valido = '1234' codigo = str ( input ( 'Introduz o Código: ' )) Confirmar = str ( input ( f'Confima o montante (carregue c) \n { montante } euros \n ' )) if Confirmar == 'c' : print ( " \n Compra efectuada" ) else : print ( "Erro na operação" ) escolha= True while escolha: print ( "Iniciar Transação" ) print ( "" ) print ...

Controlo da Velocidade

escolha= True while escolha: print ( " \n " ) print ( "Veículos Ligeiros de passageiros e mistos" ) print ( """ 1.Dentro das Localidade 2.Exit/Quit/Saída """ ) escolha= input ( "Escolha uma opção: " ) if escolha== "1" : velocidade = int ( input ( "Qual é a tua velocidade? " )) if velocidade > 50 : print ( " \033 [1;31m Vai em excesso de velocidade \033 [m" ) else : print ( " \033 [1;33m Parabéns vai dentro da velocidade legal. \033 [m" )

Considerações salário

seu_salario = float ( "Intruduza o seu salário: " ) if seu_salario == 705 : print ( "O seu salário é igual ao salário mínimo. " ) elif seu_salario > 705 : diferenca = seu_salario - 705 print ( f"O seu salário é superior ao salário mínimo em { diferenca } euros" ) else : diferenca = 705 - seu_salario print ( f"O seu salário é inferior ao salário mínimo em { diferenca } euros." )

Calcular o novo valor (subindo ou descendo)

from tkinter import * root =Tk() class variacaovalores(): def __init__ ( self ): self .root = root self .janela() self .deltavalores() root.mainloop() def janela ( self ): self .root.title( "Calcular" ) self .root.geometry( "400x200" ) self .root.configure( background = "sky blue" ) self .root.resizable( False, False ) def deltavalores ( self ): # Montante self .montantes_valores = DoubleVar() self .lb_montantes_valores = Label( text = " Montantes: " , font =( "Helvetica" , '10' ) , bg = "#4c4c4c" , fg = '#ffffff' ) self .lb_montantes_valores.place( relx = 0.15 , rely = 0.05 ) self .montantes_valores_entry = Entry( textvariable = self .montantes_valores) self .montantes_valores_entry.place( relx = 0.45 , ...