当在其中一个菜单中按下按钮时,我需要加载一个单独的python文件中的测验。我尝试使用import,但是它会导致其他python文件立即运行,而不是仅在按下按钮时运行,
Lines <- "some useless log information... 1
some useless log information... 2
some useless log information... 3
some useless log information... 4
some useless log information... 5
some useless log information... 6
some useless log information... 7
some useless log information... 8
some useless log information... 9
sample,acc,count
https//:url0.com,LS4456,10
https//:url1.com,LS4465,15"
writeLines(Lines, "Minardi.csv")
答案 0 :(得分:0)
如果使用python内置函数exec会怎样?
您可以使用此功能加载文件:
def exec_file(file_name):
#open the file
with open('your/file.txt') as fh:
#read it
data = fh.read()
#execute it
exec(data)
答案 1 :(得分:0)
您可以使用importlib加载python文件。
from os import path
from importlib.util import module_from_spec, spec_from_file_location
spec = spec_from_file_location("module.name", path.join(PATH, 'file.py'))
module = module_from_spec(spec)
spec.loader.exec_module(module)
答案 2 :(得分:0)
尝试:
import FSMQuiz1
def selectTask():
screen7 = Toplevel(screen5)
screen7.geometry("600x450+686+254")
screen7.title("Select a task")
Label(screen7, text = "Please select a task...", font = ("Calbiri",14)).place(relx=0.25, rely=0.044, height=41, width=304)
Button(screen7, text = "Finite State Machines", command = lambda: FSMQuiz1).place(relx=0.15, rely=0.2, height=54, width=117)
答案 3 :(得分:0)
在定义中设置按钮命令,然后使用该命令调用它,例如:
def CallButton():
import FSMQuiz1
def selectTask():
screen7 = Toplevel(screen5)
screen7.geometry("600x450+686+254")
screen7.title("Select a task")
Label(screen7, text = "Please select a task...", font = ("Calbiri",14)).place(relx=0.25, rely=0.044, height=41, width=304)
Button(screen7, text = "Finite State Machines", command = CallButton).place(relx=0.15, rely=0.2, height=54, width=117)