如何在MainWindow(QMainWindow)中调用外部函数
时间: 2024-04-30 10:03:37 浏览: 18
要在MainWindow中调用外部函数,可以使用以下步骤:
1. 在MainWindow的构造函数中导入外部模块或文件,例如:
```python
from mymodule import myfunction
```
这将导入名为`myfunction`的函数,该函数定义在名为`mymodule`的模块中。
2. 在MainWindow中定义一个槽函数,该函数将作为GUI中的按钮或菜单项的回调函数。例如:
```python
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
# Import external module
from mymodule import myfunction
# Create button to call external function
button = QPushButton("Call My Function", self)
button.clicked.connect(self.call_my_function)
def call_my_function(self):
# Call external function
myfunction()
```
在上面的示例中,我们定义了一个名为`call_my_function`的槽函数,该函数将在按钮被点击时被调用。该函数将调用之前导入的`myfunction`函数。
注意:如果你想调用的函数不是一个标准的Python模块或文件中的函数,而是一个在外部进程中运行的函数,那么你需要使用通信机制,例如进程间通信(IPC),来与该进程进行通信。