python - Most labels update well, except for one -
i apologize pasting of code. i'm @ loss how should post question. did other answers throughout week, cannot life of me figure out. know there more have program work, i'm trying team_at_play label update. i've tried printing variables , team_at_play.set() , team_at_play = team_b.get(). print team_at_play.get() shows team turn is, label not update.
also, have put functions, coin_toss(), etc. in right place respect mainloop?
here link text file can loaded menu item: "abrir capitulo":
any appreciated!
#!/usr/local/bin/env python # -*- coding: utf-8 -*- ## recall game """this timed game of vocabulary recall focus on previous units studied. if possible, sessions have time limit, teams might compete against 1 another. teams win points according how many answers given correctly. questions randomly selected.""" import sys import time import csv import stringio import random import string tkinter import * import ttk import tkfont def score_calc (answer): ##returns point value expression in play score = 0 ##print ('answer'), answer in len(answer): ##thanks user2357112 score += 1 return score master = tk() master.title("araña en la cabaña") master.geometry=("800x800+200+200") master.configure(background="black") random import choice d={} team_a= stringvar() team_b= stringvar() team_a_score= intvar() team_b_score= intvar() team_at_play = stringvar() pregunta = stringvar() answer = stringvar() turn = 0 correct_answer = stringvar() feedback=stringvar() correct = ['!sí!', '¡muy bien!', '¡excelente!', '¡fabuloso!'] incorrect =['¡caramba!', '¡ay, ay, ay!', '¡uy!'] def select_expression(): ## returns @ random expression ##print ('select_expression beginning') # print len(d) selected_question = '' global pregunta print ('select_expression @ work') try: selected_question =random.choice(d.keys()) ##problem selecting random key pregunta.set(selected_question) print 'pregunta =', pregunta.get() answer.set(d[selected_question]) print 'answer =', answer.get() ##return pregunta user2357112 ##return answer user2357112 except indexerror: print ('error') pass ##print pregunta def coin_toss (): ## tosses coin see goes first. returns or odd integer print ('coin toss @ work.') random import randint coin_toss = randint(0,1) if coin_toss == 0: turn = 3 if coin_toss == 1: turn = 4 return turn def player_turn(): ## prompts players or teams during turns. updates scoreboard. print ('player_turn() @ work.') global team_at_play global turn while turn < 1: turn = coin_toss() team_a_score.set(0) team_b_score.set(0) print 'turn =', turn if turn %2== 0: print 'turn=',turn print ('team_b:'), team_b.get() team_at_play= team_b.get() print 'team_at_play:', team_at_play select_expression() if turn %2!= 0: print 'turn=', turn print ('team_a:'), team_a.get() team_at_play= team_a.get() print 'team_at_play:', team_at_play select_expression() def nombrar_equipos(): nombrar_equipos = toplevel() ##dialog box entering team names. nombrar_equipos.title("nombrar los equipos") first_team_label = label(nombrar_equipos,text="el primer equipo:") first_team_label.grid(column=0, row=1) second_team_label = label(nombrar_equipos,text="el segundo equipo:") second_team_label.grid(column=0, row=0) team_a_entry = entry(nombrar_equipos,width =20, textvariable=team_a) team_a_entry.grid(column=1, row=0) team_a_entry.focus_set() team_b_entry = entry(nombrar_equipos, width =20, textvariable=team_b) team_b_entry.grid(column=1, row=1) entregar_button=button(nombrar_equipos, text ="entregar", command=nombrar_equipos.destroy) entregar_button.grid(column=1,row=2) def abrir_capitulo(): ##dialog box selecting chapter loaded. #this hide main window import tkfiledialog wordlist_filename = tkfiledialog.askopenfilename(parent=master,title="archivo para abrir", defaultextension=".txt") global d d = {} open(wordlist_filename) fin: rows = (line.split('\t') line in fin) d = {row[0]:row [1] row in rows} k in d: d[k]= d[k].strip('\n') ## print ('line 68') inv_d = {v:k k, v in d.items()} ##print inv_d d.update(inv_d) print d print ('¡'), len(d), ('expresiones cargadas!') return d def check_response(*args): ##checks team's answer, rewards points if correct. if team_at_play.get() == team_a.get(): if team_answer==answer: team_a_score.set(team_a_score.get() + score_calc (d[pregunta])) turn += 1 if team_answer != answer: turn += 1 if team_at_play.get() == team_b.get(): if team_answer==answer: team_b_score.set(team_b_score.get() + score_calc (d[pregunta])) turn += 1 if team_answer != answer: turn += 1 class app: def __init__(self, master): frame = frame(master) master.puntuacion= label(master, text="araña en la cabaña", font=("american typewriter", 30),bg="black", fg="red", justify=center) master.puntuacion.grid(row=0, column=2) master.team_a_label= label(master, textvariable= team_a, font=("american typewriter", 24),bg="black", fg="red") master.team_a_label.grid(row=1, column=1) master.team_b_label= label(master, textvariable= team_b, font=("american typewriter", 24),bg="black", fg="red") master.team_b_label.grid(row=1, column=3) master.team_a_score_label= label(master, textvariable= team_a_score, font=("04b", 24),bg="black", fg="yellow").grid(row=2, column=1) # team_a_score_label= tkfont.font(family="times", size=20, weight=bold, color=red) master.team_b_score_label= label(master, textvariable= team_b_score, font=("04b", 24),bg="black", fg="yellow") master.team_b_score_label.grid(row=2, column=3) master.team_at_play_label= label(master, textvariable= team_at_play, font=("american typewriter", 24),fg="yellow", bg="black") master.team_at_play_label.grid(row=4, column=2) master.pregunta_start = label(master, text="¿cómo se traduce....?", font=("american typewriter", 24),fg="blue",bg="black") master.pregunta_start.grid(row=6, column=2) master.pregunta_finish = label(master, textvariable = pregunta, font=("american typewriter", 24),fg="green",bg="black") master.pregunta_finish.grid(row=7, column=2) master.team_answer = entry(master, width=50) master.team_answer.grid(row=8, column=2) master.team_answer.focus_set() master.feedback_label = label(textvariable= feedback, font=("american typewriter", 24),fg="green",bg="black") master.feedback_label.grid(row=9, column=2) respond_button = button(master, text="responder",bg="black", command=check_response, justify=center, borderwidth=.001) respond_button.grid(row=10, column=3) master.bind("<return>", check_response) continue_button = button(master, text="adelante", bg="black", command=player_turn) continue_button.grid(row=10, column=4) menubar = menu(master) filemenu= menu(menubar,tearoff=0) filemenu.add_command(label="nombrar equipos",command=nombrar_equipos) filemenu.add_command(label="abrir capítulo",command=abrir_capitulo) filemenu.add_separator() filemenu.add_command(label="cerrar", command=master.quit) menubar.add_cascade(label="archivo",menu=filemenu) master.config(menu=menubar) master.columnconfigure(0, weight=1) master.rowconfigure(0, weight=1) app= app(master) master.mainloop()
i had make change player_turn(). experimented changing textvariable different labels in master frame , decided problem isolated player_turn. looked @ difference between , other functions had written. there might better way set stringvar equal another, came this, create intermediate variable set team_at_play.
def player_turn(): ## prompts players or teams during turns. updates scoreboard. print ('player_turn() @ work.') # global team_at_play global turn while turn < 1: turn = coin_toss() team_a_score.set(0) team_b_score.set(0) print 'turn =', turn if turn %2== 0: print 'turn=',turn print ('team_b:'), team_b.get() playing_team= team_b.get() team_at_play.set(playing_team) print 'team_at_play:', team_at_play select_expression() if turn %2!= 0: print 'turn=', turn print ('team_a:'), team_a.get() playing_team=team_b.get() team_at_play.set(playing_team) print 'team_at_play:', team_at_play select_expression()
Comments
Post a Comment