from __future__ import with_statement from javax.swing import JFrame, JButton def save(event): with open('TEST', 'wb') as f: f.write('hello') with open('TEST2', 'wb') as f: f.write('hello') frame = JFrame(size=(100, 100), defaultCloseOperation=JFrame.EXIT_ON_CLOSE) button = JButton('Push me', mouseClicked=save) frame.add(button) frame.visible = True