前提
下記のcheckbox
プログラムの作成中にサブウィンドウのチェックボックスには初めからチェックが入らず、メインウィンドウ上でしかチェックボックスに初めからチェックが入りませんでした。サブウィンドウや、どちらのウィンドウでもチェックボックスに初めからチェックを入れるにはどうすればいいのでしょうか。
チェックボックスにチェックが入るソース
python
1from tkinter import * 2 3root = Tk() 4root.geometry("200x100") 5root.title('Check box') 6 7aroot = Tk() 8aroot.geometry("200x100") 9aroot.title('Check box') 10 11 12# callback fucntion from button 13def callback(): 14 global checked_value 15 print('Value is', checked_value.get()) 16 17# Create button 18b = Button(root, text="Show value", font=("",12), width=10, command=callback) 19b.place(x = 50, y = 20) 20 21# Create check box 22checked_value1 = BooleanVar() 23checked_value2 = BooleanVar() 24 25checked_value1.set(True) 26chk1 = Checkbutton(root, variable=checked_value1, text='value') 27chk1.pack() 28 29 30checked_value2.set(True) 31chk2 = Checkbutton(root, variable=checked_value2, text='value') 32chk2.place(x = 100, y = 60) 33 34root.mainloop() 35
チェックが入らないソース
from tkinter import * root = Tk() root.geometry("200x100") root.title('Check box') aroot = Tk() aroot.geometry("200x100") aroot.title('Check box') # callback fucntion from button def callback(): global checked_value print('Value is', checked_value.get()) # Create button b = Button(aroot, text="Show value", font=("",12), width=10, command=callback) b.place(x = 50, y = 20) # Create check box checked_value1 = BooleanVar() checked_value2 = BooleanVar() checked_value1.set(True) chk1 = Checkbutton(aroot, variable=checked_value1, text='value') chk1.pack() checked_value2.set(True) chk2 = Checkbutton(aroot, variable=checked_value2, text='value') chk2.place(x = 100, y = 60) root.mainloop()
試したこと
root.mainloop
の関係かと思い、aroot.mainloop()
を記入しても変わらず。また、
python
1root = Tk() 2root.geometry("200x100") 3root.title('Check box') 4 5aroot = Tk() 6aroot.geometry("200x100") 7aroot.title('Check box')
を
python
1aroot = Tk() 2aroot.geometry("200x100") 3aroot.title('Check box') 4 5root = Tk() 6root.geometry("200x100") 7root.title('Check box')
にしたところ、チェックボックスにチェックが入る結果が逆になりました。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/11/25 17:28 編集
2018/11/25 19:14