GET VALUE FROM SWING GUI

Asked by spyros-liakos on 2017-05-09

Hi. I can't get value from swing simple gui to the rest script
I think it must be in a different thread but I can't find any working example
Can you show me a working example??
*im running throw command line the script

My simple code is this:

from javax.swing import (JButton, JFrame, JPanel, JCheckBox, JLabel, BoxLayout,WindowConstants)
from java.awt import BorderLayout
myval = 0
#---------------------------------------------------------------
class Mytest():
 def tally(event):
  global myval
  if checkbox1.isSelected():
   myval = 2
  else:
   myval = 3
  frame.visible = False
#---------------------------------------------------------------
 frame = JFrame('Test!', defaultCloseOperation = JFrame.EXIT_ON_CLOSE,size = (300, 300))
 selectionPanel = JPanel()
 selectionPanel.layout=BoxLayout(selectionPanel, BoxLayout.Y_AXIS)
 frame.add(selectionPanel)
#---------------------------------------------------------------
 text = JLabel('---------------------------------------------')
 selectionPanel.add(text)
 text = JLabel('hello there')
 selectionPanel.add(text)
 checkbox1 = JCheckBox('2', selected = False)
 selectionPanel.add(checkbox1)
 checkbox2 = JCheckBox('3', selected = False)
 selectionPanel.add(checkbox2)
#---------------------------------------------------------------
 buttonPanel = JPanel()
 frame.add(buttonPanel, BorderLayout.SOUTH)
 button = JButton('ok', actionPerformed = tally)
 buttonPanel.add(button)
 frame.visible = True
print myval

Question information

Language:
English Edit question
Status:
Solved
For:
Sikuli Edit question
Assignee:
No assignee Edit question
Solved by:
RaiMan
Solved:
2017-05-10
Last query:
2017-05-10
Last reply:
2017-05-10
Best RaiMan (raimund-hocke) said : #1

try this:

from javax.swing import (JButton, JFrame, JPanel, JCheckBox, JLabel, BoxLayout,WindowConstants)
from java.awt import BorderLayout

myval = -1
checkbox1 = None
frame = None

def tally(event):
  global myval
  if checkbox1.isSelected():
    myval = 2
  else:
    myval = 3
  frame.setVisible(False)

def gui():
  global frame
  global checkbox1, checkbox2
  frame = JFrame('Test!', size = (300, 300))
  frame.setUndecorated(True)
  selectionPanel = JPanel()
  selectionPanel.layout=BoxLayout(selectionPanel, BoxLayout.Y_AXIS)
  frame.add(selectionPanel)
  text = JLabel('---------------------------------------------')
  selectionPanel.add(text)
  text = JLabel('hello there')
  selectionPanel.add(text)
  checkbox1 = JCheckBox('2', selected = False)
  selectionPanel.add(checkbox1)
  checkbox2 = JCheckBox('3', selected = False)
  selectionPanel.add(checkbox2)
  buttonPanel = JPanel()
  frame.add(buttonPanel, BorderLayout.SOUTH)
  button = JButton('ok', actionPerformed = tally)
  buttonPanel.add(button)
  frame.setVisible(True)

gui()
while myval < 0:
  wait(1)
print myval

spyros-liakos (spy-arts) said : #2

Thanks RaiMan, that solved my question.