pythonlisttkinter

Insert only populated entry python tkinter


for va in entries:
        outputmsg =  va.get()
        es.JournalOut(outputmsg)
        selected_ch_name.insert(i, outputmsg)

I have a series of tkinter Entry box's that take user input to select channels for some data analysis (see below),currently it reads out all of the values including empty ones,

How can I only output to console (es.JournalOut - this is an nCode command), and only insert the number in line with the relevant channel name position in list ?

The software has it's own output pipes and I want to produce lists of the users selection to specify what data to output.

Not sure I am going about it the correct way, If I have a list of channel names and the accompanying channels numbers ( not sequential channel numbers) how can I specify a subset of those into another list/other object to capture both the name and the channel number?

# Display columns of current channel numbers and Titles

    i = 0  # loops through all channel numbers to get print table value.
    while i < nchan:  # prints out all present channels with index and channel number and title #populates tables

        ch_name = tsin.GetChanTitle(i)  # loops through channel title values
        ch_num = tsin.GetChanNumber(i)  # loop through channel number titles

        ch_name_list = Label(frame, text=ch_name)  # assign values
        ch_num_list = Label(frame, text=str(ch_num))  # assign values

        ch_name_list.grid(row=i + 1, column=2)  # set label to row in grid
        ch_num_list.grid(row=i + 1, column=0)  # set label to row in grid

# Display Input boxes to get new channel numbers

        va = StringVar()  # declare a variable as type string with .get methods
        en = Entry(frame, textvariable=va)  # set en to equal an entry box within fram with input variable to store input equal to va
        en.grid(row=i + 1, column=4)  # display entry in grid

        variables.append(va)
        entries.append(en)

        i = i + 1

Solution

  • You don't even need the StringVars. Simply use entry.get(). To only print lines that are not empty, you can use if outputmsg: to check if outputmsg is not an empty string.

    Here's a small example of a program which can print the index and contents of all non-empty entries:

    import Tkinter as tk
    
    def print_non_empty():
        for i, entry in enumerate(entries):
            output = entry.get()
            if output:
                print 'Entry {}: {}'.format(i, output)
        print ''
    
    root = tk.Tk()
    entries=[]
    
    for i in range(5):
        entries.append(tk.Entry(root))
        entries[-1].pack()
    
    tk.Button(root, text='show non-empty entries', command=print_non_empty).pack()
    
    root.mainloop()