使用带有 Python GUI 的 C 应用程序
Using C application with a Python GUI
我用 C 构建了一个简单的应用程序,我希望它有一个 GUI。我希望 GUI 在 Python 中(因为它更容易)但我不确定如何从 Python 获得用户对 C 的响应,或者即使有可能。
我想要的是一个 tkinter window 打开询问用户是否要播放文件,如果他们单击“是”,则播放文件。
这是我的 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。
将 main_file.c 和 运行 编译为可执行文件,使用:os.system(r'PATH_TO_EXE')
我用 C 构建了一个简单的应用程序,我希望它有一个 GUI。我希望 GUI 在 Python 中(因为它更容易)但我不确定如何从 Python 获得用户对 C 的响应,或者即使有可能。
我想要的是一个 tkinter window 打开询问用户是否要播放文件,如果他们单击“是”,则播放文件。
这是我的 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。
将 main_file.c 和 运行 编译为可执行文件,使用:os.system(r'PATH_TO_EXE')