1

I apologize for pasting all of my code. I'm at a loss as to how I should post this question. I did look for other answers throughout this week, but I cannot for the life of me figure this out. I know that there is more I have to do to get this program to work, but I'm just trying to get the team_at_play label to update. I've tried printing the variables and team_at_play.set() and team_at_play = team_b.get(). The print team_at_play.get() shows the team whose turn it is, but the label does not update.

Also, have I put the functions, like coin_toss(), etc. in the right place with respect to the mainloop?

Here is a link to a text file that can be loaded from the menu item: "Abrir Capitulo":

http://www.mariacarrillohighschool.com/Teachers/JamesBaptista/Spanish7-8/ClassDocuments/Handouts/expres_1_1.txt

Any help would be greatly appreciated!

    #!/usr/local/bin/env python
# -*- coding: utf-8 -*-
## Recall Game

"""This will be a timed game of vocabulary recall that will focus on
all the previous units studied. If possible, the sessions will have
a time limit, so teams might compete against one another.
Teams will win points according to how many answers are given correctly.
Questions will be randomly selected."""
import sys
import time
import csv
import StringIO
import random
import string
from Tkinter import *
import ttk
import tkFont

def score_calc (answer):
##Returns a point value for the expression in play
    score = 0
    ##print ('Answer'), answer
    for i in len(answer): ##Thanks to user2357112
        score += 1
    return score 


master = Tk()
master.title("Araña en la Cabaña")
master.geometry=("800x800+200+200")
master.configure(background="black")

from 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 at random an expression
    ##print ('select_expression beginning')
#     print len(d)
    selected_question = ''
    global pregunta
    print ('select_expression at 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  Thanks to user2357112
        ##return answer    Thanks to user2357112
    except IndexError:
        print ('Error')
        pass
    ##print pregunta

def coin_toss ():
## Tosses a coin to see who goes first. Returns even or odd integer
    print ('Coin toss at work.')
    from 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() at 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 for entering the 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 for selecting the chapter to be loaded.
    #this will hide the main window
    import tkFileDialog
    WORDLIST_FILENAME = tkFileDialog.askopenfilename(parent=master,title="Archivo para abrir", defaultextension=".txt")
    global d
    d = {}
    with open(WORDLIST_FILENAME) as fin:
            rows = (line.split('\t') for line in fin)
            d = {row[0]:row [1] for row in rows}
    for k in d:
         d[k]= d[k].strip('\n')
    ## print ('Line 68')
    inv_d = {v:k for 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 a 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()
4

1 回答 1

0

我不得不对 player_turn() 进行更改。我尝试更改主框架中不同标签的文本变量,并确定问题与 player_turn 无关。然后我查看了它与我编写的其他函数之间的区别。可能有更好的方法将 StringVar 设置为等于另一个,但我想出了这个方法,即创建一个中间变量,我将其设置为 team_at_play。

def player_turn():
## Prompts players or teams during turns. Updates scoreboard.
    print ('Player_turn() at 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()
于 2013-08-24T19:05:12.523 回答