0

Hi,
I'm newbie on Jython and on this forum as well.

I need to create a GUI with Swing.
My original code is based on Tkinter (see below, code adapted from Tony Veijalainen's Tkinter Info Grid by loops and tuples).

The main point is the use of mainloop() allowing the program to enter an event loop.
Within this loop, the user can enter new values via the Entry widget.

Is there anything equivalent or similar on Jython ?
What I basically need is to create a layout that is editable and that can collect variables to list.

Thanks for help
Best regards

from Tkinter import *

data = [
('a', 0, 'liters soda'),
('b', 0, 'liters beer')
]

## Create grid, extracting infos from the data array
## Collect the text variables to list for use
def createWidgets(root, clist=[]):
    L=0
    while L < len(data):
        cg_w=DoubleVar()
        clist.append(cg_w)
        l_w=Label(root, text=data[L][2])
        l_w.grid(row=L)
        c_w=Entry(root, textvariable=cg_w, width=3)
        c_w.grid(row=L,column=1)
        L+=1

root=Tk()
root.title('Bar')
##
cl=[]

createWidgets(root,cl)

## Example of simple function using values from an edited list (cl)
def Calc():
    L=0
    v=0
    lit=0
    for L,v in enumerate(cl):
        lit+=v.get()
    TotLiters.configure(text='%g' % lit)
    
compute = Button(root, text=' Total liters = ', command=Calc)
compute.grid(row=0,column=3)

TotLiters=Label(root, width=10)
TotLiters.grid(row=0,column=4)

root.mainloop()
1
Contributor
1
Reply
8
Views
6 Years
Discussion Span
Last Post by paul.hahn
0

Here a solution that seems to work. It makes use of JTable.

from java.awt import *
from javax.swing import *
from javax.swing.table import *

from java.util import Vector


data = [
('a', 0, 'liters soda'),
('b', 0, 'liters beer')
]

## Create table, extracting infos from the data array
## Collect the cell value to list for use


class CrW:

    def Calc(self,event):
        L=0
        clist=[]
        lit=float(0.0)
        while L < len(data):
            clist.append(self.model.getValueAt(L,1))
            lit += float(self.model.getValueAt(L,1))
            L+=1
        self.TotLiters.text=str(lit)

    def __init__(self):
        frame = JFrame('Bar')
        frame.setSize(100, 100)
        frame.setLayout(BorderLayout())
        L=0
        rowdata=Vector()
        while L < len(data):
            row=Vector()
            row.addElement(data[L][2])
            row.addElement(float(0.0))
            rowdata.addElement(row)
            L+=1
        colNames=Vector()
        colNames.addElement("Drink")
        colNames.addElement("Liters")
        self.model = DefaultTableModel(rowdata,colNames)
        self.table=JTable(self.model)
        self.table.setRowHeight(self.table.getRowHeight() + 3)
        frame.add(self.table,BorderLayout.NORTH)
        compute = JButton(' Total liters = ', actionPerformed=self.Calc)
        frame.add(compute,BorderLayout.LINE_START)
        self.TotLiters=JLabel(' ')
        frame.add(self.TotLiters,BorderLayout.LINE_END)
        frame.pack()

        frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE)
        frame.setVisible(True)
        

if __name__ == '__main__':
        CrW()
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.