2016-04-25 16 views
1

私はGUIプログラミングには新しく、このPythonプログラムを動作させようとしています。関数start()を呼び出して、2つのキャンバスを更新する正しい方法は何でしょうか。私のコードはこのように見えます。Tkinterループ関数

from Tkinter import * 
class TKinter_test(Frame): 
    def __init__(self,parent): 
     Frame.__init__(self,parent) 
     self.parent = parent 
     self.initUI() 

    user_reply = 0 


    def accept(self): 
     self.user_reply = 1 
    def decline(self): 
     self.user_reply = 2 

    def initUI(self): 
     BtnFrame = Frame (self.parent) 
     BtnFrame.place(y=450, x=20) 

     canvasFrame = Frame(self.parent) 
     canvasFrame.place(y = 200) 
     canvas_1 = Canvas(canvasFrame, width = "220", height ="200") 
     canvas_2 = Canvas(canvasFrame, width = "220", height ="200") 
     canvas_1.pack(side = LEFT) 
     canvas_2.pack(side = RIGHT) 

     textfield_1 = canvas_1.create_text(30,50,anchor = 'w', font =("Helvetica", 17)) 
     textfield_2 = canvas_2.create_text(30,50,anchor = 'w', font =("Helvetica", 17)) 
     Accept = Button(BtnFrame, text="Friends!", width=25, command = self.accept) 
     Decline = Button(BtnFrame, text="Not friends...", width=25, command = self.decline) 
     Accept.pack(side = LEFT) 
     Decline.pack(side = RIGHT) 

    def ask_user(self,friend_1,friend_2): 
     timer = 0; 

     while(timer == 0): 
      if(self.user_reply == 1): 
       print friend_1 + " and " + friend_2 + " are friends!" 
       self.user_reply = 0 
       timer = 1; 
      elif(user_reply == 2): 
       print friend_1 + " and " + friend_2 + " are not friends..." 
       self.user_reply = 0 
       timer = 1 

    def start(self): 
     listOfPeople = ["John","Tiffany","Leo","Tomas","Stacy","Robin","Carl"] 
     listOfPeople_2 = ["George","Jasmin","Rosa","Connor","Valerie","James","Bob"] 
     for friend_1 in listOfPeople: 
      for friend_2 in listOfPeople_2: 
       ask_user(friend_1,friend_2) 
     print "Finished" 

def main(): 
    root = Tk() 
    root.geometry("500x550") 
    root.wm_title("Tkinter test") 
    app = TKinter_test(root) 
    root.mainloop() 

if __name__ == '__main__': 
main() 

私はtextfield_1.itemconfigure(text = friend_1)ようtextfield_1と2何かを更新ask_user何かに使用したいと私はスレッドを使用しないことを好むだろう。 ありがとうございます。

+0

更新の実行方法は不明です。 –

+0

関数 'start()'が関数 'ask_user()'を呼び出すたびに、友人リストの2つの名前でビューを更新し、ユーザーがそれらの回答を返すのを待っていますボタン付きの友人です。@BillalBEGUERADJ – Nate

+0

遅い回答をおかけして申し訳ありませんが、うまくいけば私のコードが役立ちます。 –

答えて

0

イベント駆動型のプログラミングを行うには、異なる考え方が必要です。最初は少しイライラして困惑することがあります。スタックオーバーフローに関するハイスコアのTkinter答えのコードを見て、それを試して、小さな変更を加えて何が起こるかを見てみることをお勧めします。あなたがそれにもっと慣れていくと、となります。 :)

あなたはすべてのそれらのFrameCanvasオブジェクトをしたい理由を私は知らないので、私はthis answerからシングルFrame、「借入」ブライアンオークリーのテンプレートを使用するためのGUIを簡素化しました。友だちの名前を表示するためにCanvasのテキスト項目を使用するのではなく、単なるLabelウィジェットを使用します。

フレンドリストをGUIクラスにハードコーディングする代わりに、タプルでGUIコンストラクタにキーワード引数としてfriendlistsを渡します。 kwargsFrame.__init__メソッドに渡す前に、その引数をエラーとして認識しないキーワードargsを処理するので、その引数をkwargsから削除する必要があります。

二重forループを使用してフレンド名のペアを生成するジェネレータ式self.pairsを作成します。 next(self.pairs)を呼び出すと、次のペアの友人名が得られます。

Buttonを押すとtest_friends方法はかどうかに応じて、TrueまたはFalsereply引数で呼び出された「フレンズ!」または「友人ではありません」。ボタンを押す。

test_friendsは現在の友人のペアに関する情報を表示し、次にset_friendsメソッドを呼び出して、Labelsに次の友人の名前を設定します。ペアが残っていなければ、プログラムは終了する。

import Tkinter as tk 

class MainApplication(tk.Frame): 
    def __init__(self, parent, *args, **kwargs): 
     # Extract the friend lists from the keyword args 
     friends1, friends2 = kwargs['friendlists'] 
     del kwargs['friendlists'] 

     tk.Frame.__init__(self, parent, *args, **kwargs) 
     self.parent = parent 

     # A generator that yields all pairs of people from the friends lists 
     self.pairs = ((u, v) for u in friends1 for v in friends2) 

     # A pair of labels to display the names 
     self.friend1 = tk.StringVar() 
     tk.Label(self, textvariable=self.friend1).grid(row=0, column=0) 

     self.friend2 = tk.StringVar() 
     tk.Label(self, textvariable=self.friend2).grid(row=0, column=1) 

     # Set the first pair of names 
     self.set_friends() 

     cmd = lambda: self.test_friends(True) 
     b = tk.Button(self, text="Friends!", width=25, command=cmd) 
     b.grid(row=1, column=0) 

     cmd = lambda: self.test_friends(False) 
     b = tk.Button(self, text="Not friends.", width=25, command=cmd) 
     b.grid(row=1, column=1) 

    def set_friends(self): 
     # Set the next pair of names 
     f1, f2 = next(self.pairs) 
     self.friend1.set(f1) 
     self.friend2.set(f2) 

    def test_friends(self, reply): 
     f1, f2 = self.friend1.get(), self.friend2.get() 
     reply = (' not ', ' ')[reply] 
     print '%s and %s are%sfriends' % (f1, f2, reply) 
     try: 
      self.set_friends() 
     except StopIteration: 
      # No more pairs 
      print 'Finished!' 
      self.parent.destroy() 


def main(): 
    people_1 = [ 
     "John", 
     "Tiffany", 
     "Leo", 
     "Tomas", 
     #"Stacy", 
     #"Robin", 
     #"Carl", 
    ] 

    people_2 = [ 
     "George", 
     "Jasmin", 
     "Rosa", 
     #"Connor", 
     #"Valerie", 
     #"James", 
     #"Bob", 
    ] 

    root = tk.Tk() 
    root.wm_title("Friend Info") 
    app = MainApplication(root, friendlists=(people_1, people_2)) 
    app.pack() 
    root.mainloop() 

if __name__ == "__main__": 
    main() 
+0

ありがとうございます!私は自分のプログラムがこれでうまくいくようにすることができます。 – Nate

関連する問題