Tkinter OptionMenu(Python 3) - 执行命令后重置菜单

时间:2017-07-31 08:50:33

标签: python-3.x tkinter optionmenu

亲爱的StackOverflow社区,

我正在为我的足球赛季处理计划建立一个Tkinter GUI。具体来说,我有一个包含游戏输入功能的可视化表示的类。此类包含OptionMenu小部件,分别提示用户选择团队及其分数。

这里的问题是我在提交游戏后无法解决如何重置OptionMenu的问题。在我详细说明之前,让我提供代码;

class Game_View():
'''
This class contains the visual representation of the game input feature.
'''
def __init__(self, frame, parent, season, selected_hometeam, selected_awayteam, selected_homegoal, selected_awaygoal):
    self.frame=frame
    self.parent=parent
    self.season=season
    self.insert_game_prompt()
    self.insert_input_fixture_button()
    self.selected_hometeam=selected_hometeam
    self.selected_homegoal=selected_homegoal
    self.selected_awayteam=selected_awayteam
    self.selected_awaygoal=selected_awaygoal

def insert_game_prompt(self):
    '''
    This method creates the drop-down menus that allow the user to provide information about a fixture.
    In specific, the user inputs the home team, the away team, and their scored goals.
    '''
    self.goal_list=[]
    i=0
    while i<11:
        self.goal_list.append(i)
        i+=1
    #The record for scored goals in the a Premier League match ever is 9;
    # (Man Utd v. Ipswich town, 9 - 0, 4th of March 1995.)
    # Hence, this program allows the user to exceed this record by 1, as the
    # upper limit of possible scored goals is 10.

    window_title_1=Label(self.frame, text="Home Team:", bg='grey')
    window_title_1.pack()

    self.selected_hometeam=StringVar()
    self.selected_hometeam.set('Provide Team')
    hometeam_prompt=OptionMenu(self.frame, self.selected_hometeam, *self.season.team_names, command=self.assign_hometeam)
    hometeam_prompt.config(bg='grey')
    hometeam_prompt.pack()

    self.selected_homegoal=StringVar()
    self.selected_homegoal.set('Provide Score')
    homegoal_prompt=OptionMenu(self.frame, self.selected_homegoal, *self.goal_list, command=self.assign_homegoal)
    homegoal_prompt.config(bg='grey')
    homegoal_prompt.pack()

    window_title_2=Label(self.frame, text="\nAway Team:", bg='grey')
    window_title_2.pack()

    self.selected_awayteam=StringVar()
    self.selected_awayteam.set('Provide Team')
    awayteam_prompt=OptionMenu(self.frame, self.selected_awayteam, *self.season.team_names, command=self.assign_awayteam)
    awayteam_prompt.config(bg='grey')
    awayteam_prompt.pack()

    self.selected_awaygoal=StringVar()
    self.selected_awaygoal.set('Provide Score')
    awaygoal_prompt=OptionMenu(self.frame, self.selected_awaygoal, *self.goal_list, command=self.assign_awaygoal)
    awaygoal_prompt.config(bg='grey')
    awaygoal_prompt.pack()

def assign_hometeam(self, team):
    '''
    This method updates the class attribute with the home team selected by the user.
    '''
    self.selected_hometeam=team

def assign_awayteam(self, team):
    '''
    This method updates the class attribute with the away team selected by the user.
    '''
    self.selected_awayteam=team

def assign_homegoal(self, goal):
    '''
    This method updates the class attribute with the goals scored by the home team.
    '''
    self.selected_homegoal=goal

def assign_awaygoal(self, goal):
    '''
    This method updates the class attribute with the goals scored by the away team.
    '''
    self.selected_awaygoal=goal

def insert_input_fixture_button(self):
    '''
    This method inserts a button that the user presses when he/she wants to input a match entry.
    '''
    input_fixture_button=Button(self.frame, height=1, width=10, text='Input fixture', highlightbackground='grey')
    input_fixture_button.bind('<Button-1>', self.submit_fixture)
    input_fixture_button.pack()

def submit_fixture(self, a):
    '''
    When the Insert input button is pressed, this method sends the information provided by the user to the match handling logic
    in the Season class. Secondly, it prompts a new rendering of the table. Thirdly, it resets the input fixture button.
    '''
    self.season.submit_match_data(self.selected_hometeam, self.selected_awayteam, self.selected_homegoal, self.selected_awaygoal)
    self.parent.render_season_view()

如您所见,单击Input Fixture按钮会触发submit_fixture方法,该方法在调用其他类中的匹配逻辑时使用当前选定的OptionMenu选项(因此,其类属性)。目前,单击“输入夹具”按钮后,OptionMenu选项仍然存在,但我希望它们分别重置为“提供团队(分数)”。

我试过在submit_fixture方法中调用最后一个“re-render”方法;

(1)通过.destroy()/ .forget()

销毁OptionMenus

(2)重置类属性(selected_home(away)_team(goal))

(3)调用self.insert_game_prompt()重新创建OptionMenus

但是,无论我如何配置此重新渲染方法,单击“输入夹具”按钮后选项仍然存在。首先;是否有更简单的方法来重置选项而不是销毁/创建它们?当然,我没有运气搜索论坛。其次,你对我的错误有所了解吗?

最佳, JG

0 个答案:

没有答案