Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

tkinter Button for loop

Hello I have problem with my code in python with tkinter. I am making program for transcribe DNA strand to RNA. My output should be the strand I put into the entry but transcribed to RNA so for example … input = ACGTAGCT , output UGCAUCGA ( A in DNA is U in RNA, C is G, G is C, and T is A). The problem is that there is an empty output when I click on button.

my code is here:

import tkinter as tk
from tkinter import Label, Button, filedialog, Text
import os 

root = tk.Tk()

canvas = tk.Canvas(root, height = 400, width = 400)
canvas.pack()

y = tk.Entry(root)
y.pack()
abc = y.get()

seq = [""]

for i in abc:
    if i == "A":
        seq.append("T")
    elif i == "T":
        seq.append("A")
    elif i == "C":
        seq.append("G")
    elif i == "G":
        seq.append("C") 

def click():
    y.get()
    mylabel = Label(root, text = seq)
    mylabel.pack()
    
run = Button(root, text = " Translate", bd = "5", command=click) 

run.pack(side = "top")       

popis1 = tk.Label(root, text='Insert DNA sequence')
popis1.config(font=('helvetica', 14))
canvas.create_window(200, 100, window=popis1)

popis2 = tk.Label(root, text= "RNA sequence is:")
canvas.create_window(200, 210, window=popis2)

output = tk.Label(root, text= print(seq),font=('helvetica', 10, 'bold',))
canvas.create_window(200, 230, window=output)

root.mainloop()```

Thanks for any help 

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

There are several things here, the main issue is that you are not recalculating the new result in the click function so it should look like this:

def click():
    seq = [""]
    abc = y.get()
    
    for i in abc:
        if i == "A":
            seq.append("U")
        elif i == "T":
            seq.append("A")
        elif i == "C":
            seq.append("G")
        elif i == "G":
            seq.append("C") 
            
    mylabel = Label(root, text = seq)
    mylabel.pack()

You can now remove the for loop from where it was. Also, U didn’t appear in the translation so I changed it.

Now it works but it will show {} before the result because you’re printing an array. You can solve this by using the method shown here:

mylabel = Label(root, text = ''.join(seq))

Now you can expect to see something like this:

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading