Home > Software engineering >  Tkinter button not showing the desired text
Tkinter button not showing the desired text

Time:12-22

I have a problem with my code in python with Tkinter. I am making a program for transcribing DNA strands 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 the 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()

CodePudding user response:

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 enter image description here

  • Related