在Jython Music中创建一个打开/保存文件对话框

时间:2017-12-26 18:08:29

标签: jython-music

是否可以使用JythonMusic中的GUI库来创建打开/保存文件对话框。如果不是可以在Tkinter或PyQT中使用打开的文件窗口和jythonMusic一起使用吗?

1 个答案:

答案 0 :(得分:0)

JythonMusic的GUI库构建于Java Swing之上。因此,原则上,您可以访问所有Swing功能。

以下代码执行您的要求(并演示方法)。

与Java原始版本http://www.java2s.com/Code/Java/Swing-JFC/DemonstrationofFiledialogboxes.htm

相比,请注意代码的可读性

这部分是由于Python语法的经济性,也是由于JythonMusic GUI库,它旨在简化GUI创建(对于大多数任务)。

# 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)

希望这有帮助。

相关问题