将C应用程序与Python GUI一起使用

时间:2017-09-06 19:36:51

标签: python c python-3.x user-interface python-c-api

我在C中构建了一个简单的应用程序,我希望它有一个GUI。我希望GUI能够使用Python(因为它更容易)但我不确定如何从Python到C获得用户响应,或者即使它可能也是如此。

我想要的是一个tkinter窗口打开,询问用户是否想要播放文件,如果他们点击是,则播放该文件。

这是我的C脚本(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;
}

和我的GUI.py文件:

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()

问题在于我不知道如何让Python告诉C使用Play()函数。

我在Windows 10上使用MinGW。

1 个答案:

答案 0 :(得分:2)

编译main_file.c并使用以下命令将其作为可执行文件运行:App.Current.MainPage = new NavigationPage(new MainPage())