python - Tkinter loop function -
i new gui programming , i'm trying python program work. correct way call function start()
updates 2 canvases. code looks this.
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 + " , " + friend_2 + " friends!" self.user_reply = 0 timer = 1; elif(user_reply == 2): print friend_1 + " , " + friend_2 + " 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"] friend_1 in listofpeople: 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()
i use in ask_user updates textfield_1 , 2. textfield_1.itemconfigure(text = friend_1)
, prefer not use threads. thank you.
it takes different mindset event-driven programming, , can bit frustrating , bewildering @ first. suggest take @ code in high-scoring tkinter answers on stack overflow, , experiment it, making small changes , see happens. become more familiar it will begin make sense. :)
i don't know why want frame
, canvas
objects, i've simplified gui use single frame
, "borrowing" bryan oakley's template this answer. rather using canvas
text items display friend names use simple label
widgets.
instead of hard-coding friends lists gui class pass them in tuple gui constructor keyword argument, friendlists
. have remove argument kwargs
before passing kwargs
on frame.__init__
method, since method treats keyword args doesn't recognize errors.
i create generator expression self.pairs
uses double for
loop yield pairs of friend names. calling next(self.pairs)
gives next pair of friend names.
when button
pressed test_friends
method called reply
arg of true
or false
, depending on whether "friends!" or "not friends." button pressed.
test_friends
prints information current pair of friends , calls set_friends
method set names of next pair of friends in labels
. if there no pairs left, program exits.
import tkinter tk class mainapplication(tk.frame): def __init__(self, parent, *args, **kwargs): # extract friend lists keyword args friends1, friends2 = kwargs['friendlists'] del kwargs['friendlists'] tk.frame.__init__(self, parent, *args, **kwargs) self.parent = parent # generator yields pairs of people friends lists self.pairs = ((u, v) u in friends1 v in friends2) # pair of labels display 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 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 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 , %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()
Comments
Post a Comment