如果我的代码破坏了所有内容等,我试图制作一个安全失败按钮("q"(。 因此,当我在键盘上点击"Q"时,我所有的显示器都将启用,以防万一万一变暗。我让它工作,但它很慢,所以我尝试使用多散文,但是当我点击 q 时,代码会多次启动 GUI,但它有效,所以所有屏幕再次打开。
那么如何阻止它制作更多的 GUI 窗口呢?
import tkinter as tk
# import keyboard
import time
from multiprocessing import Process
def setupWork():
os.system("C:multimonitortool-x64MultiMonitorTool.exe /disable 4")
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 3 ")
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 2 ")
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 1 ")
def setupMovie():
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 4")
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 3")
os.system("C:multimonitortool-x64MultiMonitorTool.exe /disable 2")
os.system("C:multimonitortool-x64MultiMonitorTool.exe /disable 1")
def onlyMain():
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 3")
os.system("C:multimonitortool-x64MultiMonitorTool.exe /disable 4")
os.system("C:multimonitortool-x64MultiMonitorTool.exe /disable 2")
os.system("C:multimonitortool-x64MultiMonitorTool.exe /disable 1")
# def enableAll():
# os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 4")
# print("1")
# time.sleep(1)
# os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 3")
# print("2")
# time.sleep(1)
# os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 2")
# print("3")
# time.sleep(1)
# os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 1")
# print("4")
# time.sleep(1)
# def testkey(event):
# enableAll()
def D1():
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 4")
print("1")
def D2():
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 3")
print("2")
def D3():
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 2")
print("3")
def D4():
os.system("C:multimonitortool-x64MultiMonitorTool.exe /enable 1")
print("4")
root = tk.Tk()
# fail safe if rip
# root.bind("q", testkey)
root.bind("q", D1)
root.bind("q", D2)
root.bind("q", D3)
root.bind("q", D4)
background_image = tk.PhotoImage(file= "C:/Users/ERD/Documents/codes/MultiMonitorScrips/background.png")
background_label = tk.Label(root, image=background_image)
background_label.place(relwidth=1, relheight=1)
# background_logo = tk.PhotoImage(file=)q
root.title('MMTS')
root.minsize(width=250, height=100)
button_A = tk.Button(root, text='Work', command=setupWork, bg='gray80')
button_A.place(relx=0.1, rely=0.3 ,relheight=0.35, relwidth=0.35)
button_B = tk.Button(root, text='Movie', command=setupMovie, bg='gray80')
button_B.place(relx=0.55, rely=0.3 ,relheight=0.35, relwidth=0.35)
Button_C = tk.Button(root, text='Main', command=onlyMain, bg='gray80')
Button_C.place(relx=0.35, rely=0.88, relheight=0.1, relwidth=0.3)
root.mainloop()
if __name__ == '__main__':
d1 = Process(target=D1)
d2 = Process(target=D2)
d3 = Process(target=D3)
d4 = Process(target=D4)
d1.start()
d2.start()
d3.start()
d4.start()
d1.join()
d2.join()
d3.join()
d4.join()```
根据函数中的路径,您将在窗口上运行。
您所看到的是multiprocessing
必须在ms-windows上工作的方式的副作用。在 ms-windows 上,多处理必须在所有工作进程中import
文件。该导入应运行,而不会产生副作用,例如创建窗口。
因此,您应该将除函数定义之外的所有内容移动到if __name__ is "__main__"
块中。特别是创建窗口并运行主循环。
编辑:
另外,我不确定你为什么在这里使用multiprocessing
?我认为MultiMonitorTool.exe
很快就会完成。
mainloop
后,只需致电
for n in range(4, 0, -1):
os.system(
"C:multimonitortool-x64MultiMonitorTool.exe /enable " + str(n)
)
这应该重新打开所有显示器。