ask open file with a button

Hello,
I create an interface with kxgui which contain a button.
I want to open the interface of askopenfile .obj or .stl for put it on the scene with the library PyQt5 and don’t know how can i do that.
I tried with this code:

from bge import logic as g, events as efrom kxgui import *
import bgl
from PyQt5.QtWidgets import QFileDialog 
         
def importScan():    
    fileName = QFileDialog.getOpenFileName("Open File", os.getcwd())


def main(cont):    
    o = cont.owner
    sce = g.getCurrentScene()
    suz = sce.objects["Cuve"]
    
    if "init" not in o:
        o["gui"] = GUI.new()
        o["gui"].loadTheme(g.expandPath("//kxgui/examples/theme.json"))
        
        o["gui"].cursor = Cursor.new(g.expandPath("//cursor1.png"))


        box = o["gui"].addWidget("box", Window.new(x=0.02, y=0.02, width=0.2, height=0.9))
        
        IMPORT = box.addWidget("IMPORT", Button.new(text="IMPORT SCAN", height=0.05))
    
    ##############
    Appel function
    ##############
        IMPORT.events.callbacks["on_click"] = importScan


    else:
        o["gui"].update()
        widgets = o["gui"].widgets["box"].widgets

Can you help me please?

i dont know anything about what your trying to do, but for better readability, i recommend putting each import on its own line. using * importing and single letter variables are frowned upon.

you simply can’t

The BGE uses .blend files not .obj or .stl.
Using a complete different GUI within the BGE is a bit strange too. It looks like you are in a different game engine somehow.

i want to use .obj or .stl on .blend file. kxgui is a package that i create un interface with buttons with.
i look for make an action for my button that can open and read a file (.obj or .stl) on .blend.
i hope you understand me;

I understand you.

What do you think should the BGE do with the file path to the .obj file?

open it on the scene BGE

The BGE does not know what a .obj file is. Therefore it will not do anything with it.

You can use .blend files. They can be used within LibLoad().

Thanks for your reply. I think i am not clear. i already import .obj on my scene with this "bpy.ops.import_mesh.stl(filepath=“C://Users//admin//Desktop//file.obj”) ".
I create a button on kxgui that import this file.obj but not in real time (i close exit game engine to see my object on the scene) and when i run game engine for the second time, i see the file.obj on the scene of game engine. (i don’t know why it don’t appear in the first time)
Now, i want to import any file .obj (tkinter has askopenfile() function that can do that but i can’t import tkinter labrary on blender) in real time with game engine.
So this is my problem

This is not BGE API.

This is Blender API. You imported the file into the Blender scene.

Imagine your game is a car. You can start it, steer it, open doors and so on.

When your game is a car - Blender is the car factory. It provides the engine, wheels, a hull and doors. It is up to you how Blender assembles the different parts to form your custom car.

What you did is following: you sit in the car and while driving you tell the factory to add a fancy roof. Your current car does not change. The factory changes the blue print for a car that is all.

To see the changes you trash your car and let the factory create a new one from the changed blue prints … this new car has the fancy roof. You might think the car changes … but in reality you simply got a new one.

Imagine what happens when you have no factory. You will stick to the car as is. And that is the situation of your customers. Without Blender you do not have access to BPY.

Thanks for you unteresting explication.
as for you, what is the best solution to do the modify my car (my game) by staying in the factory (blender)?
is there any solution to interact with the game using bpy?

When you want to use bpy you have to deliver Blender and run the game embedded. (without Blender the Blender API [bpy] can’t work).

This is an option but surely not the best one. If you want to stay with it, you do not need a different GUI framework, you can use GUI of Blender.

But I do not know your high level goal. So I can’t give advice if that fits the situation you are looking for.

I already create my project with just blender API and GUI of blender. and all work good.
Icreate with GUI of blender some buttons that interact with the object (boolean, decimate, wireframe, subdivision and solidify functions)
I look for embed this blender application on the web or PyQt or Swing java to add some interfaces of identification and registration …
That is why i look for convert it to a game engine application to embed it in another application. But if you have other solutions to offer them I will thank you infinitely.

To be honest, the BGE does not fit your requirements.

It does not load .obj files. It does not embed into other applications.

Usually I do not recommend to use other game engines. But your situation is different.

It seams you prefer java. I suggest to have a look at jMonkey. It is already java (I do not know the supported import formats). As far as I remember they managed to run it as component in an eclipse view. So you can have your game inside eclipse.

Btw. I still do not know why your requirements are like that.

Thank you. I already test jMonkey but programming in blender is very easy to programming in jMonkey and java and also i already program all in blender that’s why i prefere found a solution with blender related with other ide.