Tkinter循环功能

Tkinter循环功能

问题描述:

我是新的GUI编程,我试图让这个Python程序工作。调用函数start()以便更新两个画布的正确方法是什么?我的代码看起来像这样。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() 

我想在ask_user东西,更新textfield_1和2喜欢的东西textfield_1.itemconfigure(text = friend_1)使用,我宁愿不使用线程。 谢谢。

+0

目前尚不清楚您希望如何执行更新。 –

+0

那么,每次函数'start()'调用函数ask_user()时,我都希望它使用好友列表的两个名字来更新视图,并等待用户给出他们是否应答的答案有按钮的朋友接受/拒绝@BillalBEGUERADJ – Nate

+0

对不起,迟到的回应,但希望我的代码会有所帮助。 –

它采用不同的思维方式进行事件驱动编程,起初可能有点令人沮丧和困惑。我建议你看看Stack Overflow的高分Tkinter答案中的代码,并对它进行试验,做一些小改动,看看会发生什么。随着你越来越熟悉它,开始有意义。 :)

我不知道为什么你想所有这些FrameCanvas对象,所以我已经简化了GUI使用单个Frame,“借”布莱恩·奥克利的模板从this answer。而不是使用Canvas文本项目来显示朋友名称,我只是使用简单的Label小部件。

而不是将好友列表硬编码到GUI类中,我将它们作为关键字参数传递给GUI构造函数friendlists。我们必须在将kwargs传递给Frame.__init__方法之前从kwargs中删除该参数,因为该方法将关键字参数视为错误。

我创建了一个生成器表达式self.pairs,它使用双重for循环来产生一对朋友名称。拨打next(self.pairs)给了我们下一对朋友的名字。

Button按下test_friends方法被调用了reply arg的TrueFalse,取决于是否“朋友们!”或“不是朋友”。按钮。

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