Run event on Tkinter button

Asked

Viewed 79 times

0

I’m having trouble getting the button to run a def and make a simple encryption and then display on the screen.

import tkinter as tk
from tkinter import *

class Application(): 
    def __init__(self, toplevel): 
        toplevel.title('teste')
        self.fr1 = Frame(toplevel) 
        self.fr1.pack() 
        self.fr2 = Frame(toplevel) 
        self.fr2.pack() 
        self.fr3 = Frame(toplevel) 
        self.fr3.pack() 
        self.fr4 = Frame(toplevel,pady=10) 
        self.fr4.pack() 
        self.fr5 = Frame(toplevel,pady=10) 
        self.fr5.pack()

        Label(self.fr1,
        text='Msg corpo ', 
        fg='darkblue',font=('Verdana','12','bold'), heigh=5).pack()
        Label(self.fr1,
        text='msg instrução', 
        fg='darkblue',font=('Verdana','8','bold'), heigh=5).pack()

        fonte1=('Verdana','10','bold')
        Label(self.fr2,text='Digite a mensagem: ',font=fonte1,width=20).pack(side=LEFT)  
        self.nome=Entry(self.fr2,width=50,font=fonte1)
        self.nome.focus_force()
        self.nome.pack(side=LEFT)

        Label(self.fr3,text='Chave: ', font=fonte1, width=20).pack(side=LEFT)
        self.cifra=Entry(self.fr3, width=15, font=fonte1)
        self.cifra.pack()

        self.travar=Button(self.fr4, text='Cripitografar', fg='black', )
        self.destravar=Button(self.fr4, text='Descriptografar', fg='black', )
        self.travar.pack(side=LEFT)
        self.destravar.pack(side=RIGHT)

        Label(self.fr5,text=' = ', font=fonte1, width=20).pack(side=LEFT)
        self.msg=Label(self.fr5, width=50, font=fonte1)
        self.msg.pack()

        def travar():
                nomes = self.nome.get()  
                cifras = self.cifra.get()
                for i in range(len(nome)):
                    print(chr(ord(nome[i]) + cifra), end='')
                print('')
      
root = tk.Tk()
app = Application(root)
root.mainloop()
#raiz=Tk() 
##Janela(raiz) 
#raiz.mainloop()

1 answer

1

Rodrigo follows below a small example:

from tkinter import *
root = Tk()

def muda_title(nome=""):
    root.title(f'Hello {nome}')

muda_title("Tkinter")
button = Button(master=root, text='Muda o titulo', command=lambda: muda_title("Rodrigo"))
button.pack()

I changed some of the code you did, but it still needs to be improved.

import tkinter as tk
from tkinter import *

class Application():
    def __init__(self, toplevel):
        toplevel.title('teste')
        self.fr1 = Frame(toplevel)
        self.fr1.pack()
        self.fr2 = Frame(toplevel)
        self.fr2.pack()
        self.fr3 = Frame(toplevel)
        self.fr3.pack()
        self.fr4 = Frame(toplevel,pady=10)
        self.fr4.pack()
        self.fr5 = Frame(toplevel,pady=10)
        self.fr5.pack()

        Label(self.fr1,
        text='Msg corpo ',
        fg='darkblue',font=('Verdana','12','bold'), heigh=5).pack()
        Label(self.fr1,
        text='msg instrução',
        fg='darkblue',font=('Verdana','8','bold'), heigh=5).pack()

        self.fonte1=('Verdana','10','bold')
        Label(self.fr2,text='Digite a mensagem: ',font=self.fonte1,width=20).pack(side=LEFT)
        self.nome=Entry(self.fr2,width=50,font=self.fonte1)
        self.nome.focus_force()
        self.nome.pack(side=LEFT)

        Label(self.fr3,text='Chave: ', font=self.fonte1, width=20).pack(side=LEFT)
        self.cifra=Entry(self.fr3, width=15, font=self.fonte1)
        self.cifra.pack()

        self.trv = Button(self.fr4, text='Cripitografar', fg='black', command=lambda: self.travar())
        self.des = Button(self.fr4, text='Descriptografar', fg='black', command=lambda: self.travar())
        self.trv.pack(side=LEFT)
        self.des.pack(side=RIGHT)
        self.msg = Label(self.fr5,text=f' = ', font=self.fonte1, width=20)
        self.msg.pack(side=LEFT)

    def travar(self):
        self.msg.destroy()
        nomes = self.nome.get()
        cifras = self.cifra.get()
        mensagem = "".join([chr(ord(caracter) + ord(cifras)) for caracter in nomes])
        self.msg = Label(self.fr5,text=f' = {mensagem}', font=self.fonte1, width=20)
        self.msg.pack(side=LEFT)

root = tk.Tk()
app = Application(root)
root.mainloop()

What should be improved

  1. self.trv and self.des are calling the same function. However the second should be one to decrypt, or unlock
  2. The brake function expects chave is only one character, if you put more than one in chave will generate an exception that must be handled.

However

The code answers the question of execute a function from a button

I hope it helps.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.