定义内的“对勾”按钮不会显示为“开”

时间:2019-07-05 03:53:09

标签: python tkinter

我有一个复选框列表,我希望在放置它们时可以通过其他条件将其设置为开或关。

在代码的顶层时,它可以正常工作。

如果我将代码放在def中以便可以调用它,则复选框不会显示。当我检查变量的状态时,它们都已正确设置,但没有显示。

这有效:

from tkinter import *
from tkinter import ttk, Label
from tkinter import Menu
from tkinter import messagebox
from tkinter import simpledialog
from tkinter import Text, TOP, BOTH, X, N, LEFT
from tkinter.ttk import Frame, Label, Entry
from tkinter.filedialog import askopenfilename
import tkinter as tk


def Main():
    # Window title
    root.title("0.4 (Alpha)")
    # Window size
    root.geometry("650x300+100+100")


root = Tk()
Main()

org1 = IntVar()
org2 = IntVar()
org3 = IntVar()
org_list = {'A' : '1st Choice', 'B': '2nd Choice', 'C': 'NIN'} 
org_x = 350
org_y = 40
label3 = Label(root, text="Choose Organizations to select from:")
label3.place(x=org_x, y=20)
if org_list.get("A") != "NIN":
    C1 = Checkbutton(root, text=org_list.get("A"), variable=org1, onvalue=1, offvalue=0)
    C1.place(x=org_x, y=org_y)
    org1.set(True)
    org_y += 20
if org_list.get("B") != "NIN":
    C2 = Checkbutton(root, text=org_list.get("B"), variable=org2, onvalue=1, offvalue=0)
    C2.place(x=org_x, y=org_y)
    org2.set(True)
    org_y += 20
if org_list.get("C") != "NIN":
    C3 = Checkbutton(root, text=org_list.get("C"), variable=org3, onvalue=1, offvalue=0)
    C3.place(x=org_x, y=org_y)
    org3.set(True)
    org_y += 20
org_values = str(org1.get())+str(org2.get())+str(org3.get())
print(org_values)

root.mainloop()

这不是:

def DrawBoxes():
    org1 = IntVar()
    org2 = IntVar()
    org3 = IntVar()
    org_list = {'A' : '1st Choice', 'B': '2nd Choice', 'C': 'NIN'} 
    org_x = 350
    org_y = 40
    label3 = Label(root, text="Choose Organizations to select from:")
    label3.place(x=org_x, y=20)
    if org_list.get("A") != "NIN":
        C1 = Checkbutton(root, text=org_list.get("A"), variable=org1, onvalue=1, offvalue=0)
        C1.place(x=org_x, y=org_y)
        org1.set(True)
        org_y += 20
    if org_list.get("B") != "NIN":
        C2 = Checkbutton(root, text=org_list.get("B"), variable=org2, onvalue=1, offvalue=0)
        C2.place(x=org_x, y=org_y)
        org2.set(True)
        org_y += 20
    if org_list.get("C") != "NIN":
        C3 = Checkbutton(root, text=org_list.get("C"), variable=org3, onvalue=1, offvalue=0)
        C3.place(x=org_x, y=org_y)
        org3.set(True)
        org_y += 20
    org_values = str(org1.get())+str(org2.get())+str(org3.get())
    print(org_values)


def Main():
    # Window title
    root.title("0.4 (Alpha)")
    # Window size
    root.geometry("650x300+100+100")
    DrawBoxes()

root = Tk()
Main()

root.mainloop()

在两种情况下,print(org_values)都将给出“ 110”,但是复选框不会检查代码是否位于def内。

0 个答案:

没有答案