Button does not call function correctly

Asked

Viewed 608 times

1

from tkinter import *

class Application:

      def __init__(self, master=None):
          self.inicio = Frame(master)
          self.inicio.pack()
          self.msg = Label(self.inicio, text="Deseja exprimentar a versao input?")
          self.msg["font"] = ("Calibri", "9", "italic")
          self.msg.pack ()
          self.sim = Button(self.inicio)
          self.sim["text"] = "sim"
          self.sim["font"] = ("Calibri", "9")
          self.sim["width"] = 10
          self.sim.bind("<Button-1>", self.Verificar)
          self.sim.pack ()
          self.nao = Button(self.inicio)
          self.nao["text"] = "nao"
          self.nao["font"] = ("Calibri", "9")
          self.nao["width"] = 10
          self.nao.bind("<Button-2>", self.Verificar)
          self.nao.pack ()

      def Verificar(self, event):
          if self.msg["text"] == "Deseja exprimentar a versao input?":
              self.msg["text"] = "Bem vindo a versao de input"

          else:
              self.msg['text']='Deseja exprimentar a versao input?'

root = Tk()

Application(root)

root.mainloop()

Can someone explain to me how to do when to click the 'Welcome to output version' message'?

  • Well, you’re creating the Label in the constructor with that phrase (that is, with "input" in it), so it is natural that it appears already. You can put another text (empty "", maybe) in the start label, for example.

  • I wish that when I click on the not that the phrase changes to welcome the output version and n I’m getting only appears when I load the second time

1 answer

1


In this passage:

self.nao.bind("<Button-2>", self.Verificar)

<Button-2> refers to the middle mouse button (try your program as is by clicking the middle mouse button).

Without touching your program too much, I would add a new function:

  def Verificar(self, event): # removi o else dessa
          self.msg["text"] = "Bem vindo a versao de input"
  def Verificar2(self, event): # função nova
          self.msg["text"] = "Bem vindo a versao de output"

And change the button code nao for <Button-1> also:

self.nao.bind("<Button-1>", self.Verificar2)

This should do what you want, using the correct buttons... :)

  • thank you very much :) solved now yes I can continue in my project

  • :D When you can, @Ghostcode, mark the reply as 'accept', by clicking on the checkmark on the left side of the text!

  • already now you know how to explain to me how to do when to click yes appear a table to insert values I have done the whole structure of table ect... but n I can make you open table n I know how to make the link between the first part and the second . I have to perform a for? cycle or an if ?

  • @Ghostcode I suggest you open a question about it, and include your code! I swear I would tell you if I knew :(

  • thanks anyway I’ve updated a previous question about the program I was doing I’ll see if anyone can help me :)

  • @Ghostcode I’ll take a look! I find it very cool that you are already leaving for a program with GUI (graphical interface) but I suggest you try to do it in console first (search by console program), and then, with a basic program working this way, try to implement a GUI for it.

  • thanks for the advice and help but it has to be right here because after I finish I will have to present in a class .:)

Show 2 more comments

Browser other questions tagged

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