Python Tkinter MenuButton

时间:2013-12-03 19:23:15

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

我正在尝试用Tkinter创建一个初学者程序来模拟NFL游戏。我在使用Tkinter创建“记分牌”时遇到困难。下面的代码在一个窗口中显示部分记分板,在另一个窗口中显示两个菜单按钮。为什么这些在单独的窗口?我正在尝试将两个菜单按钮显示在“Away Team”和“Home Team”旁边,以便用户可以选择他们想要的团队。目前还不担心计算,只是尝试启动GUI并首先运行。有什么建议?谢谢!

from Tkinter import *
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']

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

        global root
        root = tk.Tk()
        root.geometry("%dx%d+%d+%d" % (330, 80, 200, 150))
        root.title("tk.Optionmenu as combobox")

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

        sf = "value is %s" % var.get()
        root.title(sf)

        master.title("NFL Simulator")

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

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

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

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

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

    def calc(self):
        TeamA = int(self.txtTeamA.get())
        TeamB = int(self.txtTeamB.get())

        self.lblTeamA["text"] = random.randint(3,45)
        self.lblTeamB["text"] = random.randint(3,45)

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

if __name__ == "__main__":
   main()

1 个答案:

答案 0 :(得分:0)

你的根是主人。即:

global root
root = master

OptionMenu应与其他标签贴在同一框架上,以便将它们展示在一起:

self.txtTeamA = tk.OptionMenu(self.frame, var, *teams)

“我怎样才能使它们彼此独立?”

简单来说,您应该使用两个不同的变量来存储所选的选项。在您的代码中,它们都与var相关联,因此更改一个OptionMenu更改var会更改另一个OptionMenu

varA = tk.StringVar(root)
varA.set('Arizona Cardinals')

varB = tk.StringVar(root)
varB.set('Baltimore Ravens')

self.txtTeamA = tk.OptionMenu(root, varA, *teams)

self.txtTeamB = tk.OptionMenu(root, varB, *teams)