tkinter 按钮调用其他py的函数
时间: 2024-01-03 07:03:37 浏览: 79
可以通过以下步骤在 tkinter 按钮中调用其他 Python 文件中的函数:
1. 在 tkinter 文件中导入其他 Python 文件,例如,如果要调用 `other_file.py` 文件中的函数,则可以使用以下代码:
```python
import other_file
```
2. 创建一个函数,该函数将在 tkinter 按钮中调用。此函数应调用 `other_file.py` 中的函数。例如:
```python
def call_other_file_function():
other_file.function_name()
```
3. 在 tkinter 中创建一个按钮,并将 `call_other_file_function` 函数分配给按钮的 `command` 参数。例如:
```python
button = tkinter.Button(window, text="Call Other File Function", command=call_other_file_function)
```
完整代码示例:
```python
import tkinter
import other_file
def call_other_file_function():
other_file.function_name()
window = tkinter.Tk()
button = tkinter.Button(window, text="Call Other File Function", command=call_other_file_function)
button.pack()
window.mainloop()
```