Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/apache-flex/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python UnboundLocalError:局部变量';输出';分配前参考_Python_Tkinter - Fatal编程技术网

Python UnboundLocalError:局部变量';输出';分配前参考

Python UnboundLocalError:局部变量';输出';分配前参考,python,tkinter,Python,Tkinter,我必须为我在学校的生物课写一个程序。它应该“翻译”四个字母a、C、U和G的三重组合[X代表a、C、U和G可以站在那里的可能性]。GCX就是一个例子。。GCX是丙氨酸的三倍体 程序应该获得输入(tripplet)并在我的GUI(tkinter)中的标签中打印这个tripplet的氨基酸 为了方便起见,我只在示例中加入了GCX和丙氨酸-即使我在条目中键入了“GCX”,标签中也应打印“丙氨酸[Ala]” from tkinter import * import tkinter as tk # Int

我必须为我在学校的生物课写一个程序。它应该“翻译”四个字母a、C、U和G的三重组合[X代表a、C、U和G可以站在那里的可能性]。GCX就是一个例子。。GCX是丙氨酸的三倍体

程序应该获得输入(tripplet)并在我的GUI(tkinter)中的标签中打印这个tripplet的氨基酸

为了方便起见,我只在示例中加入了GCX和丙氨酸-即使我在条目中键入了“GCX”,标签中也应打印“丙氨酸[Ala]”

from tkinter import *
import tkinter as tk

# Interface Construction

# Basic Interface
root = Tk()
root.title("Genetic Translator")
root.geometry("300x175")

# Solid Label "Information for Input"
s_label2 = Label(root, text = "\nInput Tripplet which decodes for an amino acid:\n")
s_label2.pack()

# Mainentry line (tripplet = trip)
trip = Entry(root)
trip.pack()

# .upper() Function
trip = str(trip)
trip = trip.upper()

# Output Function (Trans:    trip -in- AS)
def Input():
    output = tk.StringVar(output)
    o_screen.configure(text = (output.get()))

if trip == "GCX":
    output = "Alanine [Ala]"
    Input()
else:
    output = "Unknown tripplet!"

# Space Label 1
space_label1 = Label(root)
space_label1.pack()

# Button "Confirm"
mainbutton = Button(root, text = "Confirm", command = Input)
mainbutton.pack()

# Space Label 2
space_label2 = Label(root)
space_label2.pack()

# Output Screen
o_screen = Label(root)
o_screen.pack()

# Mainloop function for Interface Options
mainloop()

在函数内创建局部变量输出并试图在创建它之前访问它时,代码会出错。更改函数中的名称将修复此错误:

def Input():
    out = tk.StringVar(output)
    o_screen.configure(text = (out.get()))
这意味着将使用您在if/else块中创建的全局
输出
,但您的代码仍然不能执行您想要的操作

使用dict将输入映射到输出,从条目中获取文本要容易得多:

root = Tk()
root.title("Genetic Translator")
root.geometry("300x175")

# Solid Label "Information for Input"
s_label2 = Label(root, text = "\nInput Tripplet which decodes for an amino acid:\n")
s_label2.pack()

trip = Entry(root)
trip.pack()


output = {"GCX":"Alanine [Ala]"}
# Output Function (Trans:    trip -in- AS)
def Input():
    o_screen.configure(text=(output.get(trip.get(),"Unknown tripplet!")))

# Space Label 1
space_label1 = Label(root)
space_label1.pack()

# Button "Confirm"
mainbutton = Button(root, text = "Confirm", command = Input)
mainbutton.pack()

# Space Label 2
space_label2 = Label(root)
space_label2.pack()

# Output Screen
o_screen = Label(root)
o_screen.pack()

# Mainloop function for Interface Options
root.mainloop()

使用
“Unknown tripplet!”
作为
dict.get
的默认参数将意味着如果用户在dict中输入任何您没有的按键,则将显示该按键

output=tk.StringVar(output)
不会将全局
output
放入StringVar。在一个函数中,一个名字要么全是本地的,要么全是全局的,而不是两者都是。哦,是的,它工作得很好:多亏了你,它帮了我很多忙。。谢谢:)那我怎样才能在这本词典中加入更多的词条呢?例如,“AUG”给出了“蛋氨酸[Met]”的输出?如何在我的条目上设置“.upper()”-命令?@J.Doe,您可以添加更多配对
k:v