¿ªÔÆÌåÓý

Locked Re: How to terminate python script


 

Fred,

Here is my take.

import java
import javax.swing
import jarray
import jmri

class TestA(jmri.jmrit.automat.AbstractAutomaton) :
def doClose(self, event):
print("Close")
# **** close command here ****
self.f.setVisible(False)
self.f.dispose()
self.stop()

def setup(self):
print "Setup"
self.f = javax.swing.JFrame("TEST",preferredSize = (200,80))
self.f.contentPane.setLayout(javax.swing.BoxLayout(self.f.contentPane,javax.swing.BoxLayout.Y_AXIS))
self.f.setDefaultCloseOperation(javax.swing.JFrame.DO_NOTHING_ON_CLOSE)
self.f.setLocationRelativeTo(None)
relButton = javax.swing.JButton("CLOSE WINDOW")
relButton.actionPerformed = self.doClose
panel1 = javax.swing.JPanel(preferredSize = (170,30))
panel1.add(relButton)
self.f.contentPane.add(panel1)
self.f.pack()
self.f.show()
self.start()
return

def handle(self):
self.waitMsec(10000)
print "loop"
return True

a = TestA()
a.setName("TestA Code")
a.setup()

Since this thread is about terminating a script, I included the stop() call. I also changed the ON CLOSE to do nothing instead of DISPOSE. Otherwise the script keeps running without a window, which might be appropriate in some cases.

Since this class is an AbstractAutomaton, I added the handle() method so that stop() does not create an error.


Dave Sand

On Aug 15, 2018, at 9:14 AM, Fred Miller via Groups.Io <tractionfan@...> wrote:

Bob, thanks for your patient response to my query. Unfortunately, I still am not "getting it." Would you be so kind as to make what ever changes are necessary to close the window in the following script:

import java
import javax.swing
import jarray
import jmri
class TestA(jmri.jmrit.automat.AbstractAutomaton) :
def doClose(self,event):
print("Close")
# **** close command here ****
return
def setup(self):
print "Setup"
f = javax.swing.JFrame("TEST",preferredSize = (200,80))
f.contentPane.setLayout(javax.swing.BoxLayout(f.contentPane,javax.swing.BoxLayout.Y_AXIS))
f.setDefaultCloseOperation(javax.swing.JFrame.DISPOSE_ON_CLOSE)
f.setLocationRelativeTo(None)
self.relButton = javax.swing.JButton("CLOSE WINDOW")
self.relButton.actionPerformed = self.doClose
self.panel1 = javax.swing.JPanel(preferredSize = (170,30))
self.panel1.add(self.relButton)
f.contentPane.add(self.panel1)
f.pack()
f.show()
self.start()
return
a = TestA()
a.setName("TestA Code")
a.setup()

Join [email protected] to automatically receive all group messages.