0

I have built a simple application in C and I want it to have a GUI. I want the GUI to be in Python (since it's easier) but am not sure how I would get the user response from Python to C or even if it's possible.

What I want is a tkinter window to open asking if the user wants to play a file and if they click on yes then for the file get played.

This is my C script (main_file.c):

#include <window.h>
#include <stdio.h>
#include <conio.h>
#include <python.h>

int Play()
{
    PlaySound("Sound1.wav", NULL, SND_ASYNC);

    while(1)
    {
        continue;
    }
    return 0;
}

int main()
{
    char filename[] = "GUI.py";
    FILE* fp;

    Py_Initialize();

    fp = _Py_fopen(filename, "r");
    PyRun_SimpleFile(fp, filename);

    Py_Finalize();
    return 0;
}

and my GUI.py file:

import tkinter as tk

class App(tk.Frame):
    def __init__(self, master=None):
        super().__init__(master)
        self.pack()
        self.option()

    def option(self):
        self.opt = tk.Button(self)
        self.opt["text"] = "Do You Want To Play File"
        self.opt["command"] = self.play
        self.opt.pack(side="top")

        self.quit = tk.Button(self, text="QUIT", fg="red", command=root.destroy)
        self.quit.pack(side="bottom")

    def play(self):
        #This part needs to tell C to use Play() function

root = tk.Tk()
app = App(master=root)
app.mainloop()

The problem is that I don't know how to make Python tell C to use the Play() function.

I'm using MinGW on Windows 10.

Xantium
  • 11,201
  • 10
  • 62
  • 89
  • Why not use Python for all? And the rest is C basics and can be implemented different ways. So what is your **specific** problem you did not find in your beginner's C book and the Python docs? – too honest for this site Sep 06 '17 at 19:39
  • 1
    What you want is creating python wrapper for C function. You can read about wrappers here : http://intermediate-and-advanced-software-carpentry.readthedocs.io/en/latest/c++-wrapping.html – Mariusz Sep 06 '17 at 20:31
  • @Mariusz I'll take look. – Xantium Sep 06 '17 at 20:58
  • @Olaf take a look at line 19 in GUI.py there is my **specific problem** or read the second last line in my question. – Xantium Sep 06 '17 at 23:15
  • @Simon: Read [ask] and follow the advice. I will not count lines, etc. As I already wrote, it does not make any sense to call a C program just to play a sound. You are making your life much hard than necessary and your code much less maintainable. – too honest for this site Sep 06 '17 at 23:21
  • Oh, and I forgot: C is not a scripting language and for Python it depends on the usage. – too honest for this site Sep 06 '17 at 23:25
  • @Olaf Point taken about the counting lines however I have mentioned it in two other places in the question. Also my script above is an overkill just to play sound however it is a [minimal](https://stackoverflow.com/help/mcve) example for the question I am asking. I don't get the comment on the scripting language. Also thanks for the advice to help improve my questions and posts. – Xantium Sep 06 '17 at 23:49

1 Answers1

2

Compile main_file.c and run it as a executable using: os.system(r'PATH_TO_EXE')

JJAACCEeEKK
  • 194
  • 1
  • 11