在Ubuntu中,.profile正在运行所需的Python脚本,但是脚本没有像通常那样工作?

2024-05-15 11:50:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用Python为Ubuntu创建一个背景转换器,主要是为了好玩。当我从shell运行脚本时,它工作并在特定文件夹中的图像之间交替显示背景。现在我正在尝试通过.profile运行脚本,以便它在登录时运行。脚本肯定正在运行--我在相关函数中放置了一个print语句,当我点击'alt,ctrl,f3'时,我会看到print语句的输出--但是不管出于什么原因,当以这种方式调用函数时,背景不会改变。我也不认为这是从非本地目录运行它的问题——正如我所说,如果我运行“python program/python/desktop\u changer”/主.py,桌面转换器运行正常。你知道吗

以下是函数的相关部分:

if __name__ == "__main__":
    time.sleep(15)
    os.chdir(os.path.dirname(os.path.realpath(__file__)))
    while True:

    #Set pic, set timer, run continously
    for img in os.listdir('pics/'):
        #This next line is functional when I hit 'alt, ctrl, f3', outputting the paths for new images every ten seconds
        print(os.path.abspath('pics/' + img))
        set_gnome_wallpaper(os.path.abspath('pics/' + img))
        time.sleep(float(10))

并设置\u gnome \u wallpaper():

def set_gnome_wallpaper(file_path):
    command = "gsettings set org.gnome.desktop.background picture-options 'zoom' && gsettings set org.gnome.desktop.background picture-uri file://'%s'" % file_path
    subprocess.call(command, shell=True)

在~/.profile中:(SPE和20是不相关的参数,它们的用法会被注释掉,直到我可以正确运行它)

python /home/chessie/program/python/desktop_changer/main.py SPE 20 &

Tags: path函数脚本imgosshellprofilefile