LoginSignup
0
0

More than 5 years have passed since last update.

【Tkinter】Radiobuttonで、選択されたもの以外を選択できない状態(disable)にする

Posted at

前提

Pythonの基礎文法がある程度理解できている
Tkinterについての知識がある(なければこちらをどうぞ)

プログラム

#!/usr/bin/env python
# -*- coding: utf8 -*-
import sys
from Tkinter import *
import tkMessageBox


root = Tk()
root.title(u"Software Title")
root.geometry("400x300")


# 状態の変更
def change_state():
    # チェックされているラジオボタンを取得
    checked = v.get()

    if ( checked == 1 ):
        # radio1がチェックされていたら
        radio2.configure( state = "disabled" )
        radio3.configure( state = "disabled" )

    elif ( checked == 2 ):
        # radio2がチェックされていたら
        radio1.configure( state = "disabled" )
        radio3.configure( state = "disabled" )


    elif ( checked == 3 ):
        # radio3がチェックされていたら
        radio1.configure( state = "disabled" )
        radio2.configure( state = "disabled" )

    else:
        print "error"




# ラジオボタンのグループ
v = IntVar()
v.set(0)


# ラジオボタン
radio1 = Radiobutton(text = u"項目1", variable = v, value = 1, command = change_state)
radio1.pack()

radio2 = Radiobutton(text = u"項目2", variable = v, value = 2, command = change_state)
radio2.pack()

radio3 = Radiobutton(text = u"項目3", variable = v, value = 3, command = change_state)
radio3.pack()


root.mainloop()
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0