Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to select all checkboxes from second window by pressing auto button in the first window.Can anyone solve this?

What I have tried:

<pre lang="Python"><pre>

from tkinter import *
import tkinter as tk

class Win1(tk.Frame):
     def __init__(self, parent, controller):
         tk.Frame.__init__(self, parent)
         Label(self, text= "WINDOW", font= ('Helvetica 20 bold')).grid(row=1, 
                column=4,padx=300, pady=50)
         b1=Button(self, text= "AUTO").grid(row=2, column=4, padx=300, 
                   pady=50)
         b1=Button(self, text= "MANUAL", 
                    command=lambda:controller.show_frame(Win2)).grid(row=3, 
                    column=4, padx=300, pady=50)
         b1=Button(self, text="Proceed", bg="green", fg="white").grid(row=5, 
                   column=4, padx=300, pady=30)


class Win2(tk.Frame):
      def __init__(self, parent, controller):
          tk.Frame.__init__(self, parent)
          val_x=200
          val_y=10
          self.vars = []
          self.array = []
          for i in range(32):
              self.array.append("RELAY " + str(i+1))
              self.vars.append(StringVar())
              self.vars[-1].set(0)
              c1=Checkbutton(self, text=self.array[i], 
                              variable=self.vars[-1], onvalue=1, 
                              offvalue=0).place(x=val_x, y=val_y)
              if i<=16:
                 val_x=val_x+0
                 val_y=val_y+20
              if i>16<=32:
                 val_x=300
                 val_y=val_y+20
              if i==15:
                 val_y=10
                 val_x=300
              if i>32<=48:
                 val_x=400
                 val_y=val_y+0
              if i==31:
                 val_y=10
                 val_x=400

          b1=Button(self, text="back", bg="red", fg="white", 
               command=lambda:controller.show_frame(Win1)).place(x=520,y=350)
          b2=Button(self, text="proceed", bg="green", 
                      fg="white").place(x=620,y=350)
          b3=Button(self, text="select all", bg="brown", fg="white", 
                    command=self.toggle_all).place(x=720,y=350
      def toggle_all(self):
          val_x=200
          val_y=10
          for i in range(0,32):
              self.vars[-1].set(1)
              c2=Checkbutton(self, text=self.array[i],variable=self.vars[-1],
                        onvalue=1, offvalue=0).place(x=val_x, y=val_y)
              if i<=16:
                 val_x=val_x+0
                 val_y=val_y+20
              if i>16<=32:
                 val_x=300
                 val_y=val_y+20
              if i==15:
                 val_y=10
                 val_x=300
              if i>32<=48:
                 val_x=400
                 val_y=val_y+0
              if i==31:
                 val_y=10
                 val_x=400
class Application(tk.Tk):
      def __init__(self, *args, **kwargs):
          tk.Tk.__init__(self, *args, **kwargs)
          window = tk.Frame(self)
          window.pack(side = "top", fill = "both", expand = True)
          self.frames = {}
          for F in (Win1, Win2):
              frame = F(window, self)
              self.frames[F] = frame
              frame.grid(row = 0, column=0, sticky="nsew")
              self.show_frame(Win1)
    
      def show_frame(self, window):
          frame = self.frames[window]
          frame.tkraise()
          self.title("Relay Test") 
          self.geometry('1500x1500')
app = Application()
app.mainloop()
Posted
Comments
Richard MacCutchan 7-Feb-23 10:14am    
You need to pass the window2 reference to window1 when you create them. then when you click a button yo call the window2 method to set or clear all the checkboxes.
Member 15916612 17-Feb-23 4:38am    
okk thankyou. I did that!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900