10

python と tkinter を使用して、チェック ボックスで選択されたプログラムを実行するプログラムを作成しようとしています。

import sys
from tkinter import *
import tkinter.messagebox
def runSelectedItems():
    if checkCmd == 0:
        labelText = Label(text="It worked").pack()
    else:
        labelText = Label(text="Please select an item from the checklist below").pack()

checkBox1 = Checkbutton(mGui, variable=checkCmd, onvalue=1, offvalue=0, text="Command  Prompt").pack()
buttonCmd = Button(mGui, text="Run Checked Items", command=runSelectedItems).pack()

それがコードですが、なぜ機能しないのかわかりませんか?

ありがとう。

4

1 に答える 1

16

IntVar変数に を使用する必要があります。

checkCmd = IntVar()
checkCmd.set(0)
def runSelectedItems():
    if checkCmd.get() == 0:
        labelText = Label(text="It worked").pack()
    else:
        labelText = Label(text="Please select an item from the checklist below").pack()

checkBox1 = Checkbutton(mGui, variable=checkCmd, onvalue=1, offvalue=0, text="Command  Prompt").pack()
buttonCmd = Button(mGui, text="Run Checked Items", command=runSelectedItems).pack()

他のニュースでは、イディオム:

widget = TkinterWidget(...).pack()

あまり良いものではありません。この場合、によって返されるものであるため、widget常に になりますNoneWidget.pack()。一般に、ウィジェットを作成し、2 つの別々のステップでジオメトリ マネージャーを認識させる必要があります。例えば:

checkBox1 = Checkbutton(mGui, variable=checkCmd, onvalue=1, offvalue=0, text="Command  Prompt")
checkBox1.pack()
于 2013-04-29T17:56:24.297 に答える