簡體   English   中英

Python for循環但不執行循環

[英]Python for loop but no loop is performed

在下面的代碼中,我有一個帶OptionMenu的GUI和一個單擊后可執行功能的按鈕。 當我從下拉列表中選擇第一個選項,然后單擊按鈕時,它應該遍歷我的statements數組中的每個項目。

例如,我想要以下組合,每個組合將觸發按鈕后面的功能(產生結果並寫入文件中的新行)。

  1. 第一選擇-第一陳述
  2. 第一選擇-第二陳述
  3. 第一選擇-第三陳述

稍后選擇“第二個選項”時,它將再次遍歷“第一條/第二條/第三條”語句。

也許我的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()
statements = ["First Statement", "Second Statement", "Third Statement"]
for statement in statements:
    x2 = var2.get()

除了代碼中不存在var2 (這會導致示例錯誤)外,該statement從未在循環體內使用,因此,由於存在3次迭代,因此會發生以下情況:

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

這不是真正有用,也不會受到之前選擇選項的影響。

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()

這可行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM