我试图通过点击结束进程创建按钮来结束在tkinter上运行的多脚本的过程。但是,当任何脚本在无限循环中运行以停止进程时,这似乎不起作用。
这是我的代码:
import os
import sys
from tkinter import *
import tkinter.messagebox as tkmsg
root = Tk(className=" x") # creates root window
# all components of thw window will come here
Label(root,text='Enter Load Value or Choose a test action',font=("Helvetica", 16), width=40, height=2).pack(side=TOP)
svalue = StringVar() # defines the widget state as string
w = Entry(root,textvariable=svalue).pack(side=LEFT,padx=10) # adds a textarea widget
def act():
print ("Value entered")
print ('%s' % svalue.get())
def script_1():
os.system('python script1_1.py')
def script_2():
os.system('python script1_2.py')
global root
while 1:
root.update()
pass
def script_3():
os.system('python script1_3.py')
def script_4():
os.system('python script1_4.py')
def script_5():
os.system('python script1_5.py')
def quit():
tkmsg.showinfo("Stopped", "Stopped")
sys.exit
if __name__ == '__main__':
global root
root = Frame ()
root.pack ()
button0 = Button(root,text="Load", command=act).pack(side=LEFT,padx=10)
button1 = Button(root,text="Test 1",command=script_1).pack(side=LEFT,padx=10)
button2 = Button(root,text="Test 2",command=script_2).pack(side=LEFT,padx=10)
button3 = Button(root,text="Test 3",command=script_3).pack(side=LEFT,padx=10)
button4 = Button(root,text="Test 4",command=script_4).pack(side=LEFT,padx=10)
button5 = Button(root,text="Test 5",command=script_5).pack(side=LEFT,padx=10)
button6 = Button(root,fg="red",text="End process",command=quit).pack(side=LEFT,padx=10)
button.mainloop() # an event loop to invoke custom function "act"
root.mainloop() # To keep GUI window running