Tkinter 按钮不显示所需的文本
Tkinter button not showing the desired text
我在 python 中使用 Tkinter 的代码有问题。我正在制作一个将 DNA 链转录为 RNA 的程序。我的输出应该是我放入条目但转录为 RNA 的链,例如...输入 = ACGTAGCT,输出 UGCAUCGA(DNA 中的 A 是 RNA 中的 U,C 是 G,G 是 C,T 是 A)。问题是当我点击按钮时有一个空的输出。
我的代码在这里:
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()
这里有几件事,主要问题是您没有在点击函数中重新计算新结果,因此它应该如下所示:
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()
您现在可以从原来的位置删除 for 循环。还有,U没有出现在翻译里,所以我改了。
现在可以使用了,但是它会在结果之前显示 {}
,因为您正在打印一个数组。您可以使用显示的方法解决此问题 here:
mylabel = Label(root, text = ''.join(seq))
现在你可以期待看到这样的东西:
我在 python 中使用 Tkinter 的代码有问题。我正在制作一个将 DNA 链转录为 RNA 的程序。我的输出应该是我放入条目但转录为 RNA 的链,例如...输入 = ACGTAGCT,输出 UGCAUCGA(DNA 中的 A 是 RNA 中的 U,C 是 G,G 是 C,T 是 A)。问题是当我点击按钮时有一个空的输出。
我的代码在这里:
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()
这里有几件事,主要问题是您没有在点击函数中重新计算新结果,因此它应该如下所示:
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()
您现在可以从原来的位置删除 for 循环。还有,U没有出现在翻译里,所以我改了。
现在可以使用了,但是它会在结果之前显示 {}
,因为您正在打印一个数组。您可以使用显示的方法解决此问题 here:
mylabel = Label(root, text = ''.join(seq))
现在你可以期待看到这样的东西: