-1
我正在嘗試導入另一個文件以在單擊按鈕時執行。所以,我有:Python/Tkinter - 如何導入文件或函數而不先執行它們?
from tkinter import *
import file
window = Tk()
button = Button(window, text='GO', command=file.function())
button.grid(column=1, row=1)
初始化窗口之前,此執行文件。我也試過:
from file import function
button = Button(window, text='GO', command=function())
但它做同樣的事情。點擊按鈕時,它們都不會被執行。如何導入文件或功能,但只有在按鈕被點擊時才執行它們?我正在使用python 3.5。 謝謝
按鈕現在工作,但導入時仍然執行的文件? – StevenH
確保您不要在文件的其他地方調用函數。此外,請確保您不要在'file.py'本身調用函數,除非在__name__ ==「__main __」:'內部。通過執行'import file','file.py'中的所有代碼都被執行,除了我提到的'if'外。我建議你多搜索一下。 – yper
完美,謝謝 – StevenH