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.

7
  • 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? Commented Sep 6, 2017 at 19:39
  • 1
    What you want is creating python wrapper for C function. You can read about wrappers here : intermediate-and-advanced-software-carpentry.readthedocs.io/en/… Commented Sep 6, 2017 at 20:31
  • @Mariusz I'll take look. Commented Sep 6, 2017 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. Commented Sep 6, 2017 at 23:15
  • @Simon: Read How to 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. Commented Sep 6, 2017 at 23:21

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.