无法使用 gui.fileOpenDlg() 创建对话框

问题描述 投票:0回答:0

Psychopy 版本:v2022.2.5

我正在尝试使用

gui.fileOpenDlg()
创建一个对话框,允许我在开始实验之前选择条件文件 (.xslx)。但不知何故,它对我不起作用。

  • 我的 .py 文件在文件夹“pilotZone”中,我的目标 .xlsx 文件都在 pilotZone/participant_stimulus_sheet 文件夹中。

  • 所有的 .xlsx 文件都被命名为“participant_stim1.xlsx, participant_stim2.xlsx...etc.”

源代码
gui.fileOpenDlg()

(来自https://psychopy.org/_modules/psychopy/gui/qtgui.html#fileOpenDlg

def fileOpenDlg(tryFilePath="",
                tryFileName="",
                prompt=_translate("Select file to open"),
                allowed=None):
    """A simple dialogue allowing read access to the file system.

    :parameters:

        tryFilePath: string
            default file path on which to open the dialog

        tryFileName: string
            default file name, as suggested file

        prompt: string (default "Select file to open")
            can be set to custom prompts

        allowed: string (available since v1.62.01)
            a string to specify file filters.
            e.g. "Text files (\\*.txt) ;; Image files (\\*.bmp \\*.gif)"
            See https://www.riverbankcomputing.com/static/Docs/PyQt4/qfiledialog.html
            #getOpenFileNames
            for further details

    If tryFilePath or tryFileName are empty or invalid then
    current path and empty names are used to start search.

    If user cancels, then None is returned.
    """
    ensureQtApp()

    if allowed is None:
        allowed = ("All files (*.*);;"
                   "PsychoPy Data (*.psydat);;"
                   "txt (*.txt *.dlm *.csv);;"
                   "pickled files (*.pickle *.pkl);;"
                   "shelved files (*.shelf)")
    fdir = os.path.join(tryFilePath, tryFileName)
    filesToOpen = QtWidgets.QFileDialog.getOpenFileNames(parent=None,
                                                         caption=prompt,
                                                         directory=fdir,
                                                         filter=allowed)
    if type(filesToOpen) == tuple:  # some versions(?) of PyQt return (files, filter)
        filesToOpen = filesToOpen[0]

    filesToOpen = [str(fpath) for fpath in filesToOpen
                   if os.path.exists(fpath)]
    if len(filesToOpen) == 0:
        return None
    return filesToOpen

我当前的代码:

# --- Import packages ---
from psychopy import locale_setup
from psychopy import prefs
from psychopy import sound, gui, visual, core, data, event, logging, clock, colors, layout
from psychopy.constants import (NOT_STARTED, STARTED, PLAYING, PAUSED,
                                STOPPED, FINISHED, PRESSED, RELEASED, FOREVER)

import numpy as np  # whole numpy lib is available, prepend 'np.'
from numpy import (sin, cos, tan, log, log10, pi, average,
                   sqrt, std, deg2rad, rad2deg, linspace, asarray)
from numpy.random import random, randint, normal, shuffle, choice as randchoice
import os  # handy system and path functions
import sys  # to get file system encoding

from psychopy.hardware import keyboard



# Ensure that relative paths start from the same directory as this script
_thisDir = os.path.dirname(os.path.abspath(__file__))
os.chdir(_thisDir)
# Store info about the experiment session
psychopyVersion = '2022.2.5'
expName = 'TLBX_Alpha0510core'  # from the Builder filename that created this script
expInfo = {
    'participant': f"{randint(0, 999999):06.0f}",
    'session': '001',
}

#--- select stimulus .xlsx file--- #
dlg = gui.fileOpenDlg(tryFilePath="pilotZone/participant_stimulus_sheet",tryFileName= ,prompt'Select file to open'= ,allowed=None)

错误信息:

## Running: /Users/jamesabcd/Desktop/TLBX_experiment/pilotZone/TLBX_Alpha0510core.py ##
  File "/Users/jamesabcd/Desktop/TLBX_experiment/pilotZone/TLBX_Alpha0510core.py", line 44
    dlg = gui.fileOpenDlg(tryFilePath="pilotZone/participant_stimulus_sheet",tryFileName= ,prompt'Select file to open'= ,allowed=None)
                                                                                          ^
SyntaxError: invalid syntax
################# Experiment ended with exit code 1 [pid:2961] #################

我怀疑我误用了这个功能,但不知道如何修复它。 :/

python dialog psychopy
© www.soinside.com 2019 - 2024. All rights reserved.