Set tkinter radio button to already selected button by default

Question:

I am creating a SOS game board game using tkinter. I have a class createBoard(tk.Tk) that handles the gui for the game. My function playerChoice(self,game_window) is used to create two sets of radio buttons with the choice ‘S’ or ‘O’ for each player, red and blue. I am trying to make it so each button defaults to an already selected ‘S’ when the game board is created. I’ve tried .invoke() as well as .set('S') on my StringVars but to no avail. playerChoice is called within createOptionsDisplay

import tkinter as tk
from tkinter import ttk
from tkinter.messagebox import showinfo
from tkinter import font
from typing import NamedTuple
from itertools import cycle

#game logic class

class createBoard(tk.Tk):
    def __init__(self,game):
        super().__init__()
        
        self.board_win = ''
        self.cells = {}
        self.title("SOS Game")
        self._game = game
        self.createOptionsDisplay()


    def playerChoice(self,game_window):
            #Blue player label and 'S' or 'O' radio buttons.
            blue_player_frame = tk.Frame(game_window)
            blue_player_label = ttk.Label(blue_player_frame, text='Blue player')
            blue_player_label.grid(row = 0, column = 0)

            blue_var = tk.StringVar()

            blue_player_s_btn = ttk.Radiobutton(blue_player_frame, text = 'S', variable = blue_var, value = 'S',command=self.setS)
            blue_player_o_btn = ttk.Radiobutton(blue_player_frame, text = 'O', variable = blue_var, value = 'O',command=self.setO)
            #does not work:
            #blue_player_s_btn.invoke()
            #blue_var.set('S')

            blue_player_s_btn.grid(row = 1, column = 0)
            blue_player_o_btn.grid(row = 2, column = 0)

            blue_player_frame.grid(row=0,column=0)

            #Red player label and 'S' or 'O' radio buttons.
            red_player_frame = tk.Frame(game_window)
            red_player_label = ttk.Label(red_player_frame, text='Red player')
            red_player_label.grid(row = 0, column = 0)

            red_var = tk.StringVar()

            red_player_s_btn = ttk.Radiobutton(red_player_frame, text = 'S', variable = red_var, value = 'S',command=self.setS)
            red_player_o_btn = ttk.Radiobutton(red_player_frame, text = 'O', variable = red_var, value = 'O', command=self.setO)
            #does not work:
            #red_player_s_btn.invoke()
            #red_var.set('S')

            red_player_s_btn.grid(row = 1, column = 0)
            red_player_o_btn.grid(row = 2, column = 0)

            red_player_frame.grid(row = 0, column = 2)
Asked By: mikedoorhandle

||

Answers:

You can initialize a variable when you define it.

blue_var = tk.StringVar(value='S')

You also need to make sure you keep a reference to the variables. You’re using local variables when you should be using instance variables (eg: self.blue_var)

Answered By: Bryan Oakley
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.