大多数标签更新,除了一个

时间:2013-08-24 04:28:39

标签: python tkinter

我为粘贴所有代码而道歉。我不知道如何发布这个问题。本周我确实寻找其他答案,但我不能为我的生活弄明白这一点。我知道我需要做的更多才能使这个程序工作,但我只是想让team_at_play标签更新。我已经尝试打印变量和team_at_play.set()以及team_at_play = team_b.get()。 print team_at_play.get()显示轮到它的团队,但标签不会更新。

另外,我是否已将函数(如coin_toss()等)放在相对于主循环的正确位置?

这是一个文本文件的链接,可以从菜单项加载:“Abrir Capitulo”:

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

非常感谢任何帮助!

    #!/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()

1 个答案:

答案 0 :(得分:0)

我不得不改变player_turn()。我尝试更改主框架中不同标签的textvariable,并确定问题被隔离到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()