Search code examples
pythoncpython-3.xuser-interfacepython-c-api

Using C application with a Python GUI


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.


Solution

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