简体   繁体   中英

Tkinter pop-up window with check button, not able to get check button states

I am making a pop up window ,on press of a button on main window. The pop up window have many check buttons, I need to get the state of the check buttons back to main window and also to make use of a select all and deselect all button. But I am getting error using the variables, and not able to pass the states back to main window.

Here is the program I wrote:

import Tkinter
import tkMessageBox

top = Tkinter.Tk()
state=[[]]

def popup(x):

    def select_clear_states():
        global Vars, all_states
        states = all_states.get()

        if states == 0:
            for I in range(len(Vars)):
                Vars[I].set(0)
        if states == 1:
            for I in range(len(Vars)):
                Vars[I].set(1)

    Vars = []

    root = Tkinter.Tk()

    all_states = Tkinter.IntVar()
    select_all = Tkinter.Checkbutton(root, text = "select/deselect all",variable = all_states, command = select_clear_states)
    select_all.grid(row = 0, column = 0, padx = 5, pady = 1)

    for n in range(10):

        var = Tkinter.IntVar()
        Vars.append(var)
        checkbox = Tkinter.Checkbutton(root, text = n+1, variable= Vars[n])
        checkbox.grid(row = n+1, column = 0, padx = 5, pady = 1)

    root.mainloop()


A = Tkinter.Button(top, text ="Hello1",command=lambda: popup(1))
A.pack()

B = Tkinter.Button(top, text ="Hello2",command=lambda: popup(2))
B.pack()

C = Tkinter.Button(top, text ="Hello3",command=lambda: popup(3))
C.pack()

top.mainloop()

am not able to make use of the variables Vars all_state and state[[]], to get he state of the check button.am getting this error

Exception in Tkinter callback
  Traceback (most recent call last):
    File "C:\Python27\lib\lib-tk\Tkinter.py", line 1536, in __call__
      return self.func(*args)
    File "C:\Users\Synapse\Documents\Neo\Tutorial\python_tutorial\button.py", line 11, in select_clear_states
      states = all_states.get()
  NameError: global name 'all_states' is not defined

I should be able to use select/deselectall button and also able to get back all states of each check button to main window.

Example code using class.

It use Toplevel() to create PopUp because Tkinter should have only one Tk() window.

And it use only one mainloop() because more loops make problem with values in variables.

It can created different number of checkboxes.

It needs more work - ie. to return values - but now you can use win.states to get states of all checkboxes (or to set them).

import Tkinter as tk
import tkMessageBox

# --- classes --- (CamelCase names)

class PopUp(tk.Toplevel):

    def __init__(self, number=10):
        tk.Toplevel.__init__(self)

        # - global checkbox to set all "small" checkboxes -

        self.global_state = tk.BooleanVar()

        cb = tk.Checkbutton(self, text="select/deselect all",
                            variable=self.global_state,
                            command=self.select_clear_states)
        cb.grid(row=0, column=0, padx=5, pady=1)

        # - "small" checkboxes -

        self.states = []

        for n in range(1, number+1):
            var = tk.BooleanVar()
            cb = tk.Checkbutton(self, text=str(n), variable=var)
            cb.grid(row=n, column=0, padx=5, pady=1)
            self.states.append(var)


    def select_clear_states(self):
        # get global checkbox
        state = self.global_state.get()

        # set all "small" checkboxes
        for x in self.states:
            x.set(state)

# --- functions --- (lower_case names)

def popup(num):

    win = PopUp(num)
    #win.states[1].set(True)

# --- main --- (lower_case names)

root = tk.Tk()

b = tk.Button(root, text="5 checkboxes", command=lambda:popup(5))
b.pack()

b = tk.Button(root, text="10 checkboxes", command=lambda:popup(10))
b.pack()

b = tk.Button(root, text="3 checkboxes", command=lambda:popup(3))
b.pack()

root.mainloop()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM