使用menubutton进行Python计算/获取函数

时间:2013-12-05 03:11:37

标签: python python-2.7 drop-down-menu widget tkinter

我在尝试为NFL模拟器设置计算时遇到问题。用户从菜单中选择一个团队,每个团队有三个变量:权力等级,胜率和运气。在进行计算时,在用户选择团队并进行模拟之后,如何提取与该团队一起的变量并将其纳入计算中?我也得到一个'AttributeError:'App'对象目前没有属性'power_rankings''错误。

from Tkinter import *
import random
try:
    import Tkinter as tk
except ImportError:
    import tkinter as tk

teams = ['Arizona Cardinals','Atlanta Falcons','Baltimore Ravens', 
'Buffalo Bills','Carolina Panthers','Chicago Bears','Cincinnati Bengals',
'Cleveland Browns','Dallas Cowboys','Denver Broncos','Detroit Lions',
'Green Bay Packers','Houston Texans','Indianapolis Colts','Jacksonville Jaguars',
'Kansas City Chiefs','Miami Dolphins','Minnesota Vikings','New England Patriots',
'New Orleans Saints','New York Giants','New York Jets','Oakland Raiders',
'Philadelphia Eagles','Pittsburgh Steelers','San Diego Chargers','San Francisco 49ers',
'Seattle Seahawks','St. Louis Rams','Tampa Bay Buccaneers','Tennessee Titans',
'Washington Redskins']

power_rankings = [10, 28, 18, 23, 6, 13, 7, 25, 8, 1, 9, 26, 31, 11, 32, 12, 21,
29, 3, 4, 17, 24, 30, 14, 15, 16, 5, 2, 20, 22, 19, 27]

winning_percentage = [.583, .250, .500, .333, .750, .500, .667, .333, .583, .833,
.583, .458, .167, .667, .250, .750, .500, .292, .750, .818, .417, .417, .333, .583,
.417, .417, .667, .909, .417, .250, .417, .250]

luck = random.randrange(0,50)

class App(object):
    def __init__(self, master):
        self.frame = Frame(master)
        self.frame.grid()

        self.headerFont = ("Helvetica", "16", "bold italic")

        global root
        root = master
        root.geometry("%dx%d+%d+%d" % (300, 180, 200, 150))
        root.title("NFL Sim")

        global var1
        global var2
        var1 = tk.StringVar(root)
        var1.set('Arizona Cardinals')
        var2 = tk.StringVar(root)
        var2.set('Arizona Cardinals')

        self.addTeams()
        self.addOutput()

    def addTeams(self):
        self.lblTitle = Label(self.frame, text = "NFL Simulator",
                              font = self.headerFont).grid(row = 0, columnspan = 2)

        self.lblTeamA = Label(self.frame,text = "Away Team")
        self.lblTeamA.grid(row = 1, column = 0, sticky = "ew")
        self.txtTeamA = tk.OptionMenu(self.frame, var1, *teams)
        self.txtTeamA.pack(side='right', padx=10, pady=10)
        self.txtTeamA.grid(row = 1, column = 1)

        self.lblTeamB = Label(self.frame,text = "Home Team")
        self.lblTeamB.grid(row = 2, column = 0, sticky = "ew")
        self.txtTeamB = tk.OptionMenu(self.frame, var2, *teams)
        self.txtTeamB.pack(side='right', padx=10, pady=10)
        self.txtTeamB.grid(row = 2, column = 1)

    def addOutput(self):
        self.btnCalc = Button(self.frame,text = "Simulate Game",command = self.calc)
        self.btnCalc.grid(row = 4, column = 0, columnspan = 2)

        self.lblScoreAPrompt = Label(self.frame,text = "Away Team Score")
        self.lblScoreAPrompt.grid(row = 5, column = 0, sticky = "ew")
        self.lblScoreA = Label(self.frame, bg = "white", anchor = "w")
        self.lblScoreA.grid(row = 5, column = 1, sticky = "ew")

        self.lblScoreBPrompt = Label(self.frame,text = "Home Team Score")
        self.lblScoreBPrompt.grid(row = 6, column = 0, sticky = "ew")
        self.lblScoreB = Label(self.frame, bg = "white", anchor = "w")
        self.lblScoreB.grid(row = 6, column = 1, sticky = "ew")

    def getTeamA(self):
        return self.getTeamA

    def getTeamB(self):
        return self.getTeamB

    def calc(self):
        PowerRank = int(self.power_rankings.get())
        WinPerc = int(self.winning_percentage.get()) * 100
        Luck = int(self.luck.get())
        HomeBonus = 10

        TeamAScore = (PowerRank + WinPerc + Luck) / 3
        TeamBScore = (PowerRank + WinPerc + Luck + HomeBonus) / 3

        self.lblScoreA["text"] = TeamAScore
        self.lblScoreB["text"] = TeamBScore


 def main():
    root = Tk()
    app = App(root)
    root.mainloop()

if __name__ == "__main__":
   main()

1 个答案:

答案 0 :(得分:0)

def calc(self):
    global var1, var2

    selected_index = teams.index(var1.get())

    PowerRank = power_rankings[ selected_index ]
    WinPerc = winning_percentage[ selected_index ] * 100
    Luck = random.randrange(0,50)
    HomeBonus = 10

    # ...

它获取了Away Team的选定名称,用它来查找teams中的索引,索引用于从power_rankingswinning_percentage获取值。主队使用var2


BTW:您可以使用global var1, var2self.var1代替self.var2。但您必须在代码中的任何地方使用self.var1self.var2