0

Trying to make a button on an application that activates a microphone (function will perform speech to text in the future) and stops using microphone when left click is released. Have the following code to do so:

######################window construction stuff#####################
def __init__(self):
        #create chat window
        self.Window = Tk()
        self.Setup()

    def run(self):
        self.Window.mainloop()

    def Setup(self):

        #building the actual core program window
        #main window code here, not needed for the question

######################window construction stuff end#####################

        ###################button start#################################

        vc= Button(self.Title,
                                text = "voice",
                                font = "sans-serif 8", 
                                #width = 20,
                                #height = 20,
                                bg = "#002654",
                                fg = "white")

        vc.bind('<ButtonPress-1>', self.voiceinput())
        vc.bind('<ButtonRelease-1>', self.voiceterminate())
       
        vc.place(relx = 0.95,
                             rely = 0.20,
                             relheight = 0.70, 
                             relwidth = 0.05)
        ###################button end#################################

...
######These are the debug versions of the functions, obviously not the final version.########
    def voiceinput(self):
        print ("acknowledged.")

    def voiceterminate(self):
        print ("terminated.")
##################debug functions end#################

Now, currently, as soon as its run it will print "acknowledged" and "terminated" to the console as soon as its finished loading the program (without clicking on anything), and subsequent clicks or releases of the "vc" button do nothing. Is there something silly I'm missing with this?

Tom Leahy
  • 11
  • 4

1 Answers1

0

You don't want to call the voiceinput and voiceterminate functions at binding time, you want to pass references to those functions to the bind. I think this may do what you need:

vc.bind('<ButtonPress-1>', self.voiceinput)
vc.bind('<ButtonRelease-1>', self.voiceterminate)

By deleting the parens, you aren't calling those functions immediately, just passing them to the bind function.

In addition, since the two functions will be called by Tkinter with event objects, they should be modified to take an additional event parameter like:

def voiceinput(self, event):
    print("acknowledged.")
user108471
  • 2,488
  • 3
  • 28
  • 41
  • 1
    Btw that will also pass in an `event` to `voiceinput` and `voiceterminate`. As `voiceinput` doesn't accept any parameters it will raise an error – TheLizzard Apr 20 '21 at 15:53
  • Good point, those functions need to be modified to take additional arguments. – user108471 Apr 20 '21 at 15:54
  • Yeah your solution fixed it. And then it pinged a load of positional argument errors as 2 are given while 1 is asked for. I'll try to fix that but thank you so much for your help. – Tom Leahy Apr 20 '21 at 15:58
  • @TomLeahy I believe adjusting the definitions of the two functions to include the `event` parameter should resolve those issues. Did you see the most recent edit of this answer? Did that work for you? – user108471 Apr 20 '21 at 16:02
  • Yeah I saw it after I typed, it worked perfectly, thank you. Out of curioisuty, does that mean that having a function who's only job is to terminate the microphone use would be totally unecessary as when the event ceases, the function would too? – Tom Leahy Apr 20 '21 at 16:02
  • You will still need to respond to the button release event with a termination action, I think, because both the `voiceinput` and `voiceterminate` functions run and complete immediately. You will probably need to reference whatever API or library you're using to communicate with the microphone for the appropriate way to start and stop it in this context. I wouldn't be able to guess myself, I'm sorry to say. – user108471 Apr 20 '21 at 16:06
  • Thats perfectly fine, at least I have confirmation on that front, thanks again, you've been very helpful. – Tom Leahy Apr 20 '21 at 16:10