How to go through Frames and get Entry widget's value

Viewed 25

I built application's interface by using for loops. I want user to input all nucleotides(one letter in one entry field), click button and other chains are generated automatically. Now I don't have any idea how to get access to value that is contained in Entry widget. I made some presentation of how this is supposed to work(how I think it's supposed to work:))enter image description here

import tkinter as tk


def generate_other_chains():
    nucleotides_list = []
    for triplet in dna1:
        if triplet != '-':
            for nucleotide in triplet:
                if triplet.get():
                    nucleotides_list.append(triplet.get())


window = tk.Tk()

#<-------complementary nucleotides dictionary------->
complementary_nucleotides_dna = {'a': 't', 't': 'a', 'c': 'g', 'g': 'c'}
complementary_nucleotides_rna = {'a': 'u', 'u': 'a', 'c': 'g', 'g': 'c'}

#<-------instruction label------->
enter_label = tk.Label(text='Fill one of the chains with nucleotides')
enter_label.grid()

#<-------first DNA chain------->
#<-------outer frame containing frames with Entry widgets on the first line------->
dna1 = tk.Frame(master=window)
for i in range(1, 10, 2):
#<-------inner frame containing 3 Entry widgets------->    
    triplet = tk.Frame(master=dna1)
    triplet.grid(row=1, column=i, pady=5)
#<-------chain's name------->
    if i == 1:
        dna1_label = tk.Label(master=triplet, text='DNA-1')
        dna1_label.pack(side=tk.LEFT)
#<-------creating 3 Entry widgets------->
    for j in range(0, 3):
        nucleotide = tk.Entry(master=triplet, width=1, font=('Helvetica', 14))
        nucleotide.pack(side=tk.LEFT)
#<-------creating separators between triplets------->
    if i != 9:
        separator_frame = tk.Frame(master=dna1)
        separator_frame.grid(row=1, column=i+1, pady=5)
        separator = tk.Label(master=separator_frame, text='-')
        separator.pack()
dna1.grid(row=1, column=0)

#<-------second DNA chain------->
#<-------outer frame containing frames with Entry widgets on the second line------->
dna2 = tk.Frame(master=window)
for i in range(1, 10, 2):
#<-------inner frame containing 3 Entry widgets------->  
    triplet = tk.Frame(master=dna2)
    triplet.grid(row=2, column=i, pady=5)
#<-------chain's name------->
    if i == 1:
        dna2_label = tk.Label(master=triplet, text='DNA-2')
        dna2_label.pack(side=tk.LEFT)
#<-------creating 3 Entry widgets------->
    for j in range(0, 3):
        nucleotide = tk.Entry(master=triplet, width=1, font=('Helvetica', 14))
        nucleotide.pack(side=tk.LEFT)
#<-------creating separators between triplets------->       
    if i != 9:
        separator_frame = tk.Frame(master=dna2)
        separator_frame.grid(row=2, column=i+1, pady=5)
        separator = tk.Label(master=separator_frame, text='-')
        separator.pack()
dna2.grid(row=2, column=0)

#<-------mRNA chain------->
#<-------outer frame containing frames with Entry widgets on the second line------->
mrna = tk.Frame(master=window)
for i in range(1, 10, 2):
#<-------inner frame containing 3 Entry widgets------->  
    triplet = tk.Frame(master=mrna)
    triplet.grid(row=3, column=i, pady=5)
#<-------chain's name------->
    if i == 1:
        dna2_label = tk.Label(master=triplet, text='MRNA')
        dna2_label.pack(side=tk.LEFT)
#<-------creating 3 Entry widgets------->
    for j in range(0, 3):
        nucleotide = tk.Entry(master=triplet, width=1, font=('Helvetica', 14))
        nucleotide.pack(side=tk.LEFT)
#<-------creating separators between triplets------->
    if i != 9:
        separator_frame = tk.Frame(master=mrna)
        separator_frame.grid(row=3, column=i+1, pady=5)
        separator = tk.Label(master=separator_frame, text='-')
        separator.pack()
mrna.grid(row=3, column=0)

#<-------tRNA chain------->
#<-------outer frame containing frames with Entry widgets on the second line------->
trna = tk.Frame(master=window)
for i in range(1, 10, 2):
#<-------inner frame containing 3 Entry widgets-------> 
    triplet = tk.Frame(master=trna)
    triplet.grid(row=4, column=i, pady=5)
#<-------chain's name------->
    if i == 1:
        dna2_label = tk.Label(master=triplet, text='TRNA')
        dna2_label.pack(side=tk.LEFT)
#<-------creating 3 Entry widgets------->
    for j in range(0, 3):
        nucleotide = tk.Entry(master=triplet, width=1, font=('Helvetica', 14))
        nucleotide.pack(side=tk.LEFT)
#<-------creating separators between triplets------->
    if i != 9:
        separator_frame = tk.Frame(master=trna)
        separator_frame.grid(row=4, column=i+1, pady=5)
        separator = tk.Label(master=separator_frame, text='-')
        separator.pack()
trna.grid(row=4, column=0)

#<-------button generating other chains of triplets------->
generate_button = tk.Button(text='Generate other chains', command=generate_other_chains)
generate_button.grid()

0 Answers
Related