有没有办法通过将它放在一个循环中来缩短这个tkinter代码?

时间:2017-03-06 22:36:08

标签: python-3.x tkinter

我希望第一个变量等同于列表中的第一个项目,然后第二个变量等于第二个项目,第三个变量等于第三个项目。我一直试图让这个循环,但它不起作用。我尝试在范围和索引中使用i,但它说事件对象不支持索引。还有其他方法可以让它成为一个循环吗?

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

1 个答案:

答案 0 :(得分: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))