我尝试做的是运行另一个文件(file1进程),当该文件正在运行时,我想检查本地目录中是否存在文件。如果文件不存在则所有内容都会继续运行,并在30秒后再次检查。如果文件确实存在,我想将内容打印到标签并停止file1进程。
下面的代码会导致Tkinter删除所有标签。我哪里错了?
def turnOn():
global proc
if proc is None:
print('Starting Beacon')
proc = subprocess.Popen(["python3", "/home/pi/FLBeacon/FLBeaconout.py"])
label1 = Label(window,text ="Beacon is Running")
label1.configure(bg='green')
label1.place(x= 350, y=10, width=150)
global label2
label2 = Label(window,text = full_message)
label2.configure(bg='green')
label2.place(x=50, y=90, width=550)
filelist = ['FLBeaconRecieved.txt']
while True:
list1 = []
for file in filelist:
list1.append(os.path.isfile(file))
if all(list1):
#all elements are true
file = open("FLBeaconRecieved.txt")
data = file.read()
file.close()
Results = Label(window, text = data)
Results.place(x = 50, y = 350)
print('Beacon Stopped')
proc.terminate()
proc = None
label1 = Label(window,text = "Beacon is not running")
label1.configure(bg='red')
label1.place(x= 350, y=10, width=150)
label2.destroy()
break
else:
time.sleep(30)
print("there is no file")
on = Button(window, borderwidth=2, text = "Start Beacon", width=15, pady=5, command = turnOn)
off = Button(window, borderwidth=2, text = "Stop Beacon", width=15, pady=5, command = turnOff)
on.place(x=215,y=300)
off.place(x=380,y=300)
def stop():
window.destroy()
#Top.destroy()
b = Button(window, borderwidth=2, text="Update Beacon", width=15, pady=5, command=enter)
b.place(x=50,y=300)
b = Button(window, borderwidth=2, text="Exit", width=12, pady=5, command = combine_funcs(turnOff, stop))
b.place(x=250,y=550)
window.mainloop()
答案 0 :(得分:0)
这似乎起作用.....
def turnOn():
global proc
if proc is None:
window.after(50000,file_chk)
print('Starting Beacon')
proc = subprocess.Popen(["python3", "/home/pi/FLBeacon/FLBeaconout.py"])
label1 = Label(window,text ="Beacon is Running")
label1.configure(bg='green')
label1.place(x= 350, y=10, width=150)
global label2
label2 = Label(window,text = full_message)
label2.configure(bg='green')
label2.place(x=50, y=90, width=550)
def file_chk():
filelist = ['FLBeaconRecieved.txt']
while True:
list1 = []
for file in filelist:
list1.append(os.path.isfile(file))
if all(list1):
#all elements are true
print("file has been found")
file = open("FLBeaconRecieved.txt")
data = file.read()
file.close()
Results = Label(window, text = data)
Results.place(x = 50, y = 350)
turnOff()
label1 = Label(window,text = "Beacon is not running")
label1.configure(bg='red')
label1.place(x= 350, y=10, width=150)
label2.destroy()
return
else:
print("there is no file")
time.sleep(30)
答案 1 :(得分:0)
必须删除while循环并将after()语句移动到else:现在它可以工作......这个特殊的代码我肯定不会工作,但它解释了这个概念。
def turnOn():
global proc
if proc is None:
window.after(50000,file_chk)
print('Starting Beacon')
proc = subprocess.Popen(["python3", "/home/pi/FLBeacon/FLBeaconout.py"])
label1 = Label(window,text ="Beacon is Running")
label1.configure(bg='green')
label1.place(x= 350, y=10, width=150)
global label2
label2 = Label(window,text = full_message)
label2.configure(bg='green')
label2.place(x=50, y=90, width=550)
def file_chk():
filelist = ['FLBeaconRecieved.txt']
list1 = []
for file in filelist:
list1.append(os.path.isfile(file))
if all(list1):
#all elements are true
print("file has been found")
file = open("FLBeaconRecieved.txt")
data = file.read()
file.close()
Results = Label(window, text = data)
Results.place(x = 50, y = 350)
turnOff()
label1 = Label(window,text = "Beacon is not running")
label1.configure(bg='red')
label1.place(x= 350, y=10, width=150)
label2.destroy()
return
else:
print("there is no file")
window.after(50000,file_chk)