2017-03-06 54 views
0

我希望第一個變量等於列表中的第一個項目,然後第二個等於第二個項目,第三個等於第三個。我一直試圖讓這個循環,但它不起作用。我試圖在範圍和索引中使用我,但它說事件對象不支持索引。有沒有其他方法可以將它變成循環?有沒有辦法縮短這個tkinter代碼通過把它放在一個循環?

import tkinter as tk 
from tkinter import * 
from random import randint 
import random 

WordsEasy1 = ["book","dictionary","paper","suitcase","jacket","box","folder","cabinet","abacus", 
"access","monitor","mouse","pencil","light","button","keyboard","country","pet","dog","love", 
"food","car","envelope","adapter","charger","board","tablet","cable","switch","case","rainbow", 
"stick","spoon","develop","hat","hands","cup","coffee","tea","letters","vase","duck","ear","fan", 
"clothes","shorts","pen","paint","alphabet","sound","money","basement","page","pause","break","fix", 
"flag","feather","speaker","message","capital","airport","tango","drink","language","type","journalist", 
"programmer","mountain","sunset","snow","ball","jersey","skirt","moon","remote","control","manual", 
"take","sign","traffic","crown","grass","garden","wallpaper","explanation","idea","pants","gym","teach", 
"bowl","shallow","deep","focus","strong","signal","process","screw","tools","quality","perseverance", 
"beauty","change","darkness","eclipse","set","bee","train","microscope","crash","interrupt","vocal", 
"can","failure","success","rate","graph","course","good","overlap","technique","yesterday","clock", 
"speed","jar","frame","fast","duty","post","mail","laundry","gift","presentation","public","media", 
"story","enhance","liberty","freedom","ownership","metal","cloud","virtual","reality","inception", 
"natural","nature","television","share","meteor","master","depart","shrimp"] 


wordeasy3a=[] 
wordeasy3b=[] 
wordeasy3c=[] 


a=0 
score=0 
wrong=0 

while a<3: 
    we1=randint(0,150) 
    wordeasyrw1=WordsEasy1[we1] 
    if wordeasyrw1 not in wordeasy3a: 
     wordeasy3a.append(wordeasyrw1) 
     a+=1 
    else: 
     pass 
if a==3: 
    a=0 

for i in wordeasy3a: 
    print(i) 


while a<3: 
    we2=randint(0,150) 
    wordeasyrw2=WordsEasy1[we2] 
    if wordeasyrw2 not in wordeasy3a: 
     wordeasy3b.append(wordeasyrw2) 
     a+=1 
    else: 
     pass 
if a==3: 
    a=0 

i=0 

print() 
for i in wordeasy3b: 
    print(i) 

root = Tk() 

def answer1(event1): 
    global score 
    global wrong 
    if event1.widget.get() == wordeasy3a[0]: 
     score+=1 
    else: 
     wrong+=1 
    print() 
    print(score) 
    print(wrong) 

def answer2(event2): 
    global score 
    global wrong 
    if event2.widget.get() == wordeasy3a[1]: 
     score+=1 
    else: 
     wrong+=1 
    print() 
    print(score) 
    print(wrong) 

def answer3(event3): 
    global score 
    global wrong 
    if event3.widget.get() == wordeasy3a[2]: 
     score+=1 
    else: 
     wrong+=1 
    print() 
    print(score) 
    print(wrong) 


def userinput(): 
    E3 = Entry(root, bd =2) 
    E3.pack (side = BOTTOM) 
    E3.bind('<Return>', answer3) 

    E2 = Entry(root, bd =2) 
    E2.pack (side = BOTTOM) 
    E2.bind('<Return>', answer2) 

    E1 = Entry(root, bd =2) 
    E1.pack (side = BOTTOM) 
    E1.bind('<Return>', answer1) 


userinput() 

root.mainloop() 

回答

0

爲什麼不只是爲它們提供一個示例函數,並將它與lambda結合起來呢?我真的不明白在第一種情況下循環是如何適應的。

def answer(event, index): 
    global score 
    global wrong 
    if event.widget.get() == list[index]: 
     score+=1 
    else: 
     wrong+=1 

def userinput(): 
    E1 = tk.Entry(root, bd=2) 
    E1.pack(side = BOTTOM) 
    E1.bind('<Return>', lambda event, arg=(0): answer(event, arg)) 

    E2 = tk.Entry(root, bd=2) 
    E2.pack(side = BOTTOM) 
    E2.bind('<Return>', lambda event, arg=(1): answer(event, arg)) 

    E3 = tk.Entry(root, bd=2) 
    E3.pack(side = BOTTOM) 
    E3.bind('<Return>', lambda event, arg=(2): answer(event, arg)) 

我想你可以在最後一位進入一個循環減少,但我不會推薦它,因爲它是最好的,如果你的部件有一個指針。

def userinput(): 
    for i in range(3): 
     temp = tk.Entry(root, bd=2) 
     temp.pack(side = BOTTOM) 
     temp.bind('<Return>', lambda event, arg=(i): answer(event, arg)) 
+0

我打算用我實際擁有的編碼來編碼。我以前沒有用過lambda。它是如何工作的? – Sirius

+0

我試過它說列表對象不可調用 – Sirius

+0

你可以給出編譯器的完整錯誤嗎?就像整個最後一行一樣。 – Dova

相關問題