[Seek Help] 设置壁纸的这个地方用变量为什么不行呢?
Tofloor
poster avatar
立青
deepin
2022-06-09 20:37
Author

python 小白,有点小错误,麻烦大神给看看原因:

mybizhi='file:///media/ccj/Files/个人/壁纸/week/'+str(myWeek)+'.png'
cmd1='dbus-send --dest=com.deepin.daemon.Appearance /com/deepin/daemon/Appearance --print-reply com.deepin.daemon.Appearance.SetMonitorBackground string:"VGA-1" string:mybizhi'#这个地方用字符串变量mybizhi为什么就不行呢?
os.system(cmd1)

Reply Favorite View the author
All Replies
whoam1
deepin
2022-06-09 21:06
#1

你需要占位符格式化字符串而不是直接写变量名

Reply View the author
蔡EEPIN
deepin
2022-06-09 21:06
#2

单引号输入什么就是什么,获取不了变量。 用双引号

Reply View the author
whoam1
deepin
2022-06-09 21:07
#3

或者直接 'path' + mybizhi

Reply View the author
咿呀
deepin
2022-06-10 06:59
#4
#顺手改了一下  😄
import os.path
path1 = 'file:///media/ccj/Files/个人/壁纸/week/'
mybizhi = os.path.join(path1,str(myWeek),'.png')
cmd1 = f'''dbus-send - -dest = com.deepin.daemon.Appearance \
/com/deepin/daemon/Appearance - -print-reply \
com.deepin.daemon.Appearance.SetMonitorBackground string: "VGA-1" \
string: {mybizhi}''' 
# 先打印一下,看命令是否完整
print(cmd1)
# os.system(cmd1)
Reply View the author