Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Tkinter toggle button to switch microphone On/Off using a script
#! /usr/bin/python
''' switchHeadphones.py
Tkinter toggle button to switch microphone On/Off using a script
Modification of the solution proposed in
https://www.daniweb.com/posts/jump/1909448
for the Mic On/Off script from
https://gist.github.com/OndraZizka/2724d353f695dacd73a50883dfdf0fc6
'''
# Define the path for the script below, e.g.,
script = "./switchHeadphones.sh"
try:
# Python2
import Tkinter as tk
except ImportError:
# Python3
import tkinter as tk
import os
__author__ = "Weslley S Pereira"
__email__ = "weslley.spereira@gmail.com"
def toggle(tog=[0]):
'''
a list default argument has a fixed address
'''
tog[0] = not tog[0]
if tog[0]:
os.system(script+' speak')
t_btn.config(text='Switch Mic Off')
else:
os.system(script+' listen')
t_btn.config(text='Switch Mic On')
root = tk.Tk()
root.title('Headphone ')
t_btn = tk.Button(text='Switch Mic On', width=15, command=toggle)
t_btn.pack(pady=5)
root.mainloop()
@prietopa

This comment has been minimized.

Copy link

@prietopa prietopa commented Sep 15, 2020

which tinker bookstore? there's a lot. https://pypi.org/search/?q=tkinter

@prietopa

This comment has been minimized.

Copy link

@prietopa prietopa commented Sep 15, 2020

sudo apt-get install python3-tk

@weslleyspereira

This comment has been minimized.

Copy link
Owner Author

@weslleyspereira weslleyspereira commented Sep 16, 2020

Thanks for the comment!

@weslleyspereira

This comment has been minimized.

Copy link
Owner Author

@weslleyspereira weslleyspereira commented Sep 28, 2020

Usage:

  1. First, you may need to change the permissions of the original Shell script switchHeadphones.sh:
    chmod +x switchHeadphones.sh
  2. Then, after connecting your headphones, execute the script Python I suggested as follows:
    python <my_script.py> &
  3. Make sure the variable script holds the correct path for the original Shell script switchHeadphones.sh.
@vasi786

This comment has been minimized.

Copy link

@vasi786 vasi786 commented Oct 5, 2020

Hi, I am getting this error when I run the python script although I installed tkinter as prietopa suggested.

Traceback (most recent call last):
  File "myscript.py", line 19, in <module>
    import tkinter as tk
ImportError: No module named tkinter
@d4niloArantes

This comment has been minimized.

Copy link

@d4niloArantes d4niloArantes commented Oct 5, 2020

@vasi786 You have to install tkinter as said by @prietopa:

sudo apt-get install python3-tk

Then run with:

python3 switchHeadphones.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment