Python for循环但没有执行循环

时间:2016-11-20 00:43:06

标签: python for-loop tkinter

在下面的代码中,我有一个带有OptionMenu的GUI和一个在单击时执行功能的按钮。当我从下拉列表中选择第一个选项然后单击该按钮时,它应该循环遍历statements数组中的每个项目。

例如,我想要以下组合,每个组合将触发按钮后面的功能(产生结果并写入文件中的新行)。

  1. 第一个选项 - 第一个声明
  2. 第一个选项 - 第二个声明
  3. 第一个选项 - 第三个声明
  4. 稍后当选择第二选项时,它将再次循环通过第一/第二/第三声明。

    也许我的for-in循环处于错误的位置,因为当我点击按钮时,文件中只记录了一个结果并且结果有误(我对此并不太担心,因为我的功能是计算句子的相似之处,以便选择错误的var / string)。它会在一次运行后停止,不会发生循环。

    这是我的代码:

    import sys
    import tkinter
    import SemanticSim
    
    from tkinter import *
    
    master = Tk()
    
    a = IntVar()
    
    var1 = StringVar(master)
    var1.set("-- Choose One Option --") # initial value
    var2 = StringVar(master)
    x3 = StringVar()
    x3.set("Click the button to get result...")
    
    master.title("Graphical User Interface")
    master.geometry("800x600")
    
    ##########################################   METHODS  ##############################################
    def ButtonClickMethod():
        a = var1.get() # get the text value of selected menu option 
        var1.set(a) # sets value to label when needed to, label needs to be set in specific manner
        x1 = var1.get()
        a = var2.get()
        var2.set(a)
        statements = ["First Statement", "Second Statement", "Third Statement"]
        for statement in statements:
            x2 = var2.get() # if I put `statement` in the bracket, I get this error: "TypeError: get() takes 1 positional argument but 2 were given"
        x3.set(SemanticSim.SemanticSimilarity(x1, x2));
    #############################################################################################
    
    b3 = Button(master, text="Calculate Results", command=ButtonClickMethod)
    b3.place(x=100, y=200)
    
    label1 = Label(master, text="Choose One Option ")
    label1.place(x=20, y=100)
    
    option1 = OptionMenu(master, var1, "First Option", "Second Option", "Third Option")
    option1.config(width=50)
    option1.pack()
    option1.place(x=200, y=100)
    
    CaptionLabelResult1 = Label(master, text="Semantic Analysis Result: ")
    CaptionLabelResult1.place(x=50, y=250)
    
    LabelResult1 = Label(master, textvariable=x3)
    LabelResult1.place(x=200, y=250)
    
    mainloop()
    

2 个答案:

答案 0 :(得分:2)

statements = ["First Statement", "Second Statement", "Third Statement"]
for statement in statements:
    x2 = var2.get()

除了你的代码中不存在var2(导致你的例子出错)之外,statement从未在循环体中使用,所以由于有三次迭代,会发生什么:

x2 = var2.get()
x2 = var2.get()
x2 = var2.get()

这不是很有用,也不会受到之前选择选项的影响。

答案 1 :(得分:0)

import sys
import tkinter
import SemanticSim

from tkinter import *

master = Tk()

a = IntVar()

var1 = StringVar(master)
var1.set("-- Choose One Option --") # initial value
var2 = StringVar(master)
x3 = StringVar()
x3.set("Click the button to get result...")

master.title("Graphical User Interface")
master.geometry("800x600")

##########################################   METHODS  ##############################################
def ButtonClickMethod():
    a = var1.get() # get the text value of selected menu option 
    var1.set(a) # sets value to label when needed to, label needs to be set in specific manner
    a = var2.get()
    var2.set(a)
    statements = ["First Statement", "Second Statement", "Third Statement"]
    for statement in statements:
        x1 = var1.get()
        x2 = statement
        x3.set(SemanticSim.SemanticSimilarity(x1, x2));
#############################################################################################

b3 = Button(master, text="Calculate Results", command=ButtonClickMethod)
b3.place(x=100, y=200)

label1 = Label(master, text="Choose One Option ")
label1.place(x=20, y=100)

option1 = OptionMenu(master, var1, "First Option", "Second Option", "Third Option")
option1.config(width=50)
option1.pack()
option1.place(x=200, y=100)

CaptionLabelResult1 = Label(master, text="Semantic Analysis Result: ")
CaptionLabelResult1.place(x=50, y=250)

LabelResult1 = Label(master, textvariable=x3)
LabelResult1.place(x=200, y=250)

mainloop()

这很有效。