cod*_*kid 5 tkinter default-value radio-button python-3.x
我正在创建一个设置窗口,但无法弄清楚如何为单选按钮设置默认值。我希望窗口以选中的黑色开头,如果用户未单击任一按钮,则仍将返回“ B”值。谢谢您的帮助。
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()
Run Code Online (Sandbox Code Playgroud)
您可以像这样为StringVar提供初始值:
self.who_goes_first = tkinter.StringVar(None, "B")
Run Code Online (Sandbox Code Playgroud)
或者您可以随时将StringVar设置为所需的值,这将更新单选按钮:
self.who_goes_first.set("B")
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10093 次 |
| 最近记录: |