【问题标题】:python 3.4 - os.system not executing the commandpython 3.4 - os.system 不执行命令
【发布时间】:2017-01-29 22:18:17
【问题描述】:

我有一些代码使用 python 创建锁屏,删除任务栏并阻止它们离开。但是,当他们获得正确的密码时,它不会使任务栏返回。该命令在cmd中有效,但在python中无效。

代码如下:

import os
from tkinter import*
import time
run = input("Do you want to lock your computer? ")
if run == "yes":
    a=Tk()
    a.overridedirect(1)
    w, h = a.winfo_screenwidth(), a.winfo_screenheight()
    a.geometry("%dx%d+0+0" % (w, h))
    os.system('taskkill /f /im  explorer.exe')
    a.attributes("-topmost", True)
    L1 = Label(a, text="Please enter the password to continue: ")
    L1.pack( side =TOP)
    Ebox = Entry(a, bd =5)
    Ebox.pack(side =TOP)
    Ebox.config(show="*");

    def check():
    if Ebox.get() == "password":
        time.sleep(0.3)
        os.system('powershell -command "Invoke-item c:\windows/explorer.exe"') # This line does not execute the command
        a.destroy()


    b = Button(a, text="submit", command=check )
    b.pack(side=TOP)
    a.mainloop()

【问题讨论】:

  • 清除代码中的一些错误后,我对其进行了测试,它似乎对我来说工作正常; os.system 正在使用该命令正确启动资源管理器。你可以通过在新窗口中使用python启动命令来具体检查它是否被执行。
  • 我只测试了 cod 以在新的 python 窗口中单独恢复任务栏,但它仍然无法正常工作。它所做的只是打开 cmd,然后它让 cmd 打开而不做任何事情。
  • 你能用 python 2.7 测试它吗?我默认启用了 python 2.7。 (另一件事是考虑到 taskkill 命令正在运行,这是程序的一个奇怪行为,而这不是,要么两者都应该工作,要么两者都不应该)

标签: python python-3.x


【解决方案1】:

清理你的代码后它对我有用:

import os
from tkinter import*
import time

run = input("Do you want to lock your computer? ")
if run == "yes":
    a=Tk()
    a.overrideredirect(1)
    w, h = a.winfo_screenwidth(), a.winfo_screenheight()
    a.geometry("%dx%d+0+0" % (w, h))
    os.system('taskkill /f /im  explorer.exe')
    a.attributes("-topmost", True)
    L1 = Label(a, text="Please enter the password to continue: ")
    L1.pack( side =TOP)
    Ebox = Entry(a, bd =5)
    Ebox.pack(side =TOP)
    Ebox.config(show="*");

    def check():
      print("Hello")
      typed=Ebox.get()
      print(typed)
      if typed == "password":
        time.sleep(0.3)
        print("Ok")
        os.system('powershell -command "invoke-item c:\windows/explorer.exe"') # this line does not execute the command
        a.destroy()

    b = Button(a, text="submit", command=check)
    b.pack(side=TOP)

    a.mainloop()

【讨论】:

  • 如果您花时间解释您所做的任何重要更改,您的答案会更好。否则,读者必须逐行逐字符地将您的解决方案与原始解决方案进行比较。
猜你喜欢
  • 1970-01-01
  • 2018-07-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-14
  • 1970-01-01
  • 2018-05-26
  • 1970-01-01
相关资源
最近更新 更多