2017-03-16 55 views
0

我正在創建一個設置窗口,我無法弄清楚如何爲一個單選按鈕設置一個默認值。我希望窗口以黑色開頭,如果用戶沒有點擊任何一個按鈕,'B'值仍然會被返回。謝謝您的幫助。在tkinter python中給一個單選按鈕設置一個默認值

import tkinter 
from tkinter import ttk 

class Test: 
    def __init__(self): 
     self.root_window = tkinter.Tk() 

     #create who goes first variable 
     self.who_goes_first = tkinter.StringVar() 

     #black radio button 
     self._who_goes_first_radiobutton = ttk.Radiobutton(
      self.root_window, 
      text = 'Black', 
      variable = self.who_goes_first, 
      value = 'B')  
     self._who_goes_first_radiobutton.grid(row=0, column=1) 

     #white radio button 
     self._who_goes_first_radiobutton = ttk.Radiobutton(
      self.root_window, 
      text = 'White', 
      variable = self.who_goes_first, 
      value = 'W')  
     self._who_goes_first_radiobutton.grid(row=1, column=1) 

    def start(self) -> None: 
     self.root_window.mainloop() 

if __name__ == '__main__': 

    game = Test() 
    game.start() 

回答

2

您可以爲您STRINGVAR提供一個初始值是這樣的:

self.who_goes_first = tkinter.StringVar(None, "B") 

,或者你可以簡單的設置STRINGVAR你想在任何時間的值,這將更新單選按鈕:

self.who_goes_first.set("B") 
相關問題