Python 如何将单选按钮选择与阵列数据关联

Python 如何将单选按钮选择与阵列数据关联,python,arrays,radio-button,tkinter,Python,Arrays,Radio Button,Tkinter,我正试图使用tkinter python 2.7编写一个GUI可搜索细菌数据库,并在代码中嵌入数据库数组。通过单选按钮选择某些条件会选择性地搜索阵列,然后点击“识别”按钮将打印出与这些选择匹配的所有细菌的id作为标签 我已经在下面发布了非功能片段[idbuttonclick(self)] 代码段def idbuttonclick(self)应该做什么: 1) 搜索名为“data”的矩阵/数组,其中第1-4列表示radiobutton字符串变量选项,每行表示细菌id 2) 选择“数据”中变量选项与

我正试图使用tkinter python 2.7编写一个GUI可搜索细菌数据库,并在代码中嵌入数据库数组。通过单选按钮选择某些条件会选择性地搜索阵列,然后点击“识别”按钮将打印出与这些选择匹配的所有细菌的id作为标签

我已经在下面发布了非功能片段[idbuttonclick(self)]

代码段def idbuttonclick(self)应该做什么:

1) 搜索名为“data”的矩阵/数组,其中第1-4列表示radiobutton字符串变量选项,每行表示细菌id

2) 选择“数据”中变量选项与单选按钮选项匹配的行

3) 如果id数为1-20,则将所选行中数据列0中的细菌id打印为self.id_框架中的标签。否则,打印消息标签“错误:数据不足”

我已获得代码,可以根据radiobutton选择为我提供id.frame标签,但仍然无法将radiobutton选择链接到阵列中的坐标,然后根据radiobutton/阵列坐标匹配提供id.frame标签

它现在是一个数组,而不是矩阵,因为矩阵、列表列表和元组显然不能使用坐标索引(i,j)存储元素

下面是我的新草稿,其中包括array和idbuttonclick命令

这应该如何工作: 如果选择了radiobutton self.gram_选项=(“+”),则数组第1列中的任何细菌名称(在同一行中的数组第2列中具有“+”值)都应显示在id.frame标签中。 因此,标签应为:“醋酸杆菌,假单胞菌属”。
此外,单击self.meta_option.get()应与第2列相对应,缩小选择范围:“fac厌氧菌”值将标记为“醋酸杆菌”,而“需氧菌”值将标记为“假单胞菌”

    data = array([
        ['Acetobacter aceti','+', 'fac anaerobe', '+', '--'],
        ['Citrobacter freundii','--', 'fac anaerobe', '+', '--'], 
        ['Pseudomonas sp','+', 'aerobe', '--', '--']])

    data.readlines()



def idbuttonclick(self, event):

    self.id_frame.destroy()
    self.id_frame = Frame(self.main_right_frame, borderwidth=5, height=50, background="white")
    self.id_frame.pack(side=TOP, fill=BOTH)

    if data[i,1]==self.gram_option.get(): 
        id = data [i,0]
        Label(self.id_frame, text = id, background = "white").pack(side=LEFT, anchor = N)

    if data[i,2]==self.shape_option.get(): 
        id = data [i,0]
        Label(self.id_frame, text = id, background = "white").pack(side=LEFT, anchor = N)

    if data[i,3]==self.meta_option.get(): 
        id = data [i,0]
        Label(self.id_frame, text = id, background = "white").pack(side=LEFT, anchor = N)

    if data[i,4]==self.cat_option.get(): 
        id = data [i,0]
        Label(self.id_frame, text = id, background = "white").pack(side=LEFT, anchor = N)

    else: Label(self.id_frame, text = 'Error: Not enough data', background = "white").pack(side=LEFT, anchor = N)
如果我没有正确说明数组坐标,或者有人知道用python代码安排上述场景的可行方法。。。 在大约两周的时间里,我得到了56条关于这个问题的意见,但是没有来自社区的代码编写反馈。希望这个新版本能更好地解决我最初的想法和需要调整的地方

真诚地


Jeff

此解决方案使用概率比较,而不是+和-(即,每个细菌有X个概率获得+分数,1-X概率获得-分数。)但此代码将单选按钮与数据矩阵关联,然后相应地对前十个得分细菌进行排名。所有部件都在配合

def idbuttonclick(self, event):

    self.id_frame.destroy()
    self.id_frame = Frame(self.main_right_frame, borderwidth=5, height=50, background="white")
    self.id_frame.pack(side=TOP, fill=BOTH)


    if (self.gram_option.get()=="+" and
    self.meta_option.get()=="aerobe"):
        Label(self.id_frame, text = "Gram Positive Aerobe", background = "white").pack(side=TOP, anchor = N)


        bac=[('Aeromonas',0.95,0.05),
        ('Bacillus',0.05, 0.95),
        ('Hafnia',0.51, 0.51)]



    else: Label(self.id_frame, text = "Error: Not enough data", background = "white").pack(side=TOP, anchor = N)

    def plus(matrix, i):
        return [row[i] for row in matrix]

    def minus(matrix, i):
        return [1.00-row[i] for row in matrix]

    def average(lst):
        return sum(lst) / len(lst)

    bact=zip(*bac)
    bact2=bact[0:1]
    bact3=bact[0:1]

    if self.cat_option.get()=="+":
        bact2.append(plus(bac,1))
        bact3.append(plus(bac,1))
    if self.cat_option.get()=="--":
        bact2.append(minus(bac,1))
        bact3.append(plus(bac,1))

    if self.oxi_option.get()=="+":
        bact2.append(plus(bac,2))
        bact3.append(plus(bac,2))

    if self.oxi_option.get()=="--":
        bact2.append(minus(bac,2))
        bact3.append(plus(bac,2))

    bac2=zip(*bact2)
    bac3=zip(*bact3)

    #experimental additive probability
    #bac4 = [(bac2[0],reduce(mul,bac2[1:])) for bac2 in bac2]

    #experimental mean probability
    bac4 = [(bac2[0], average(bac2[1:])) for bac2 in bac2]


    #experimental additive probability / expected outcome additive probability
    #bac4 = [(bac2[0],reduce(mul,bac2[1:])/reduce(mul,bac3[1:])) for (bac2,bac3) in zip(bac2,bac3)]

    bac5 = tuple(sorted(bac4, key=lambda item: item[1], reverse=True))


    Label(self.id_frame, text = bac5[0], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[1], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[2], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[3], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[4], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[5], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[6], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[7], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[8], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[9], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[10], background = "white").pack(side=TOP, anchor = W)

你有没有考虑过热情的特质?它与Python(x,y)一起提供,使设计简单的GUI变得更加容易。当你说你在“获取单选按钮和阵列交互”方面遇到问题时,你是什么意思?什么样的问题?什么样的互动?具体来说,你希望发生什么事情而没有发生?@Bill-你复制了我之前的编辑。:)后退。@razlebe:哇!我们有一个工具,我们可以点击自动转换为编辑答案。通常人们不标记和编辑,所以我们只使用该工具而不检查编辑是否已经发生。无论如何,谢谢你修好它。:)你的问题仍然很模糊。就好像你要我们为你写代码一样。你具体不明白什么?从radiobutton StringVar中获取值有困难吗?你不知道如何进行实际的搜索或打印吗?也许你可以把每一个问题都单独提出来。
def idbuttonclick(self, event):

    self.id_frame.destroy()
    self.id_frame = Frame(self.main_right_frame, borderwidth=5, height=50, background="white")
    self.id_frame.pack(side=TOP, fill=BOTH)


    if (self.gram_option.get()=="+" and
    self.meta_option.get()=="aerobe"):
        Label(self.id_frame, text = "Gram Positive Aerobe", background = "white").pack(side=TOP, anchor = N)


        bac=[('Aeromonas',0.95,0.05),
        ('Bacillus',0.05, 0.95),
        ('Hafnia',0.51, 0.51)]



    else: Label(self.id_frame, text = "Error: Not enough data", background = "white").pack(side=TOP, anchor = N)

    def plus(matrix, i):
        return [row[i] for row in matrix]

    def minus(matrix, i):
        return [1.00-row[i] for row in matrix]

    def average(lst):
        return sum(lst) / len(lst)

    bact=zip(*bac)
    bact2=bact[0:1]
    bact3=bact[0:1]

    if self.cat_option.get()=="+":
        bact2.append(plus(bac,1))
        bact3.append(plus(bac,1))
    if self.cat_option.get()=="--":
        bact2.append(minus(bac,1))
        bact3.append(plus(bac,1))

    if self.oxi_option.get()=="+":
        bact2.append(plus(bac,2))
        bact3.append(plus(bac,2))

    if self.oxi_option.get()=="--":
        bact2.append(minus(bac,2))
        bact3.append(plus(bac,2))

    bac2=zip(*bact2)
    bac3=zip(*bact3)

    #experimental additive probability
    #bac4 = [(bac2[0],reduce(mul,bac2[1:])) for bac2 in bac2]

    #experimental mean probability
    bac4 = [(bac2[0], average(bac2[1:])) for bac2 in bac2]


    #experimental additive probability / expected outcome additive probability
    #bac4 = [(bac2[0],reduce(mul,bac2[1:])/reduce(mul,bac3[1:])) for (bac2,bac3) in zip(bac2,bac3)]

    bac5 = tuple(sorted(bac4, key=lambda item: item[1], reverse=True))


    Label(self.id_frame, text = bac5[0], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[1], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[2], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[3], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[4], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[5], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[6], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[7], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[8], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[9], background = "white").pack(side=TOP, anchor = W)
    Label(self.id_frame, text = bac5[10], background = "white").pack(side=TOP, anchor = W)