DJames
DJames

Reputation: 1

Creating an open/save file dialog in Jython Music

Is it possible to use the GUI library in JythonMusic to create an open/save file dialog. If not is it possible to use an open file window in Tkinter or PyQT in conjunction with jythonMusic?

Upvotes: 0

Views: 386

Answers (1)

Bill Manaris
Bill Manaris

Reputation: 81

JythonMusic's GUI library is built on top of Java Swing. So, in principle, you can access all Swing functionality.

The code below does what you ask (and demonstrates the approach).

Do notice how readable the code is, compared to the Java original - http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm

This is partially due to the economy of Python syntax, but also due to the JythonMusic GUI library, which was designed to simplify GUI creation (for most tasks).

# openFileDialogDemo.py
#
# Demonstrates how to create an open/save file dialog in Jython Music.
#
# Based on http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm
#

from gui import *

# create display to hold open button
d = Display("Open Dialog Demo", 200, 50)

filename  = None   # holds selected filename (if any)
directory = None   # holds directory of selected filename (if any)

# set up what to do when open button is pressed
def openButtonAction():

   global filename, directory   # we will update these

   chooser = JFileChooser()     # the open dialog window

   # here is the only tricky part - accessing the GUI Display's internal JFrame, d.display
   returnValue = chooser.showOpenDialog( d.display )   

   # check what choice user made, and act appropriately
   if returnValue == JFileChooser.APPROVE_OPTION:

      filename  = chooser.getSelectedFile().getName()
      directory = chooser.getCurrentDirectory().toString()
      print "Filename =", filename
      print "Directory =", directory

   elif returnValue == JFileChooser.CANCEL_OPTION:
      print "You pressed cancel"

# create open button and add it to display
openButton = Button("Open", openButtonAction)
d.add(openButton, 70, 12)

Hope this helps.

Upvotes: 0

Related Questions