2016-07-01 22 views
0

forloopafter()メソッドを使用します。計画は1秒間隔ですべての結合されたテキストを印刷することです。after()メソッドがmulti forループで機能しないのはなぜですか?

ただし、最後まで直接実行され、最後に結合されたテキストのみが印刷されます。これをどうすれば解決できますか?私はあなたが望むものを理解していれば、あなたは自分のリストのcartestian製品が欲しい

# -*- coding: utf-8 -*- 

from Tkinter import * 
import time 

FirstList = ["1", "2", "3", "4"] 
SecondList = ["a", "b", "c", "d", "e", "f"] 
ThirdList = ["A" , "B" , "C"] 

root = Tk() 
root.title("Program") 
root['background'] ='gray' 

def command_Print(): 
    for i in range(0, len(FirstList), 1): 
     for j in range(0, len(SecondList), 1): 
      for k in range(0, len(ThirdList), 1): 
       PrintText = FirstList[i] + SecondList[j] + ThirdList[k] 
       Labelvar.set(PrintText) 
       Label0.after(1000, lambda: command_Print) 

Labelvar = StringVar() 
Labelvar.set(u'original value') 
Frame0 = Frame(root) 
Frame0.place(x=0, y=0, width=100, height=50) 
Label0 = Label(Frame0, textvariable=Labelvar, anchor='w') 
Label0.pack(side=LEFT) 

Frame_I = Frame(root) 
Frame_I.place(x = 100, y = 0, width=100, height=70) 
Button_I = Button(Frame_I, text = "Button" , width = 100, height=70, command = command_Print) 
Button_I.place(x=0, y=0) 
Button_I.grid(row=0, column=0, sticky=W, pady=4) 
Button_I.pack() 

root.mainloop() 

答えて

3

は、ここに私のコードです。あなたのようにネストされたforloopの代わりにitertoolsを使うことができます。itertoolsはすでにこの組み込み関数を持っており、はるかに洗練されているので、ホイールを再作成する必要はありません。

ここに行く:(未テスト)

import itertools 

PRODUCTS = itertools.product(FirstList, SecondList, ThirdList) 

def show_next_product(): 

    try: 
     LabelVar.set(next(PRODUCTS)) 
     root.last_after = root.after(1000, show_next_product) 
    except StopIteration: 
     LabelVar.set("Out of products.") 
     root.after_cancel(root.last_after) 

また、StringVarは不要と思われます。 StringVarのためにtraceメソッドを使って何かをしていない限り、私はそれらを正直に使うという点を見たことはありません。

Label0['text'] = 'newtext'を使用してテキストを直接変更することはできますが、それはもちろん個人的な好みです。 :)

+0

マイナーフィックス: ''' .join(next(PRODUCTS))'が必要です。 'tuple'ではなく文字列を取得します。 – ShadowRanger

関連する問題