Python Forum
[HELP] Nested conditional? double condition followed by another condition.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[HELP] Nested conditional? double condition followed by another condition.
#16
You need an extra variable to keep track of if do_some was running first or not.
I have tried creating it with tkinter does this give the functionality you are looking for
import tkinter as tk


class Toggle_button(tk.Button):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.button_name = kwargs['text']
        self.button_on = False
        self.set_button_state()
        self.bind('<Button-1>', self.on_button)

    def on_button(self, event):
        self.button_on = not self.button_on
        self.set_button_state()

    def set_button_state(self):
        button_state = 'On'
        if not self.button_on:
            button_state = 'Off'
        self.configure(text=f'{self.button_name} {button_state}')


class running_label(tk.Label):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.label_name = kwargs['text']
        self.running(False)

    def running(self, is_running=True):
        running_state = 'Running'
        if not is_running:
            running_state = 'Not Running'
        self.configure(text=f'{self.label_name}: {running_state}')


class MainFrame(tk.Frame):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.button1 = Toggle_button(self, text='Button1')
        self.button1.pack(pady=5)
        self.button2 = Toggle_button(self, text='Button2')
        self.button2.pack(pady=5)
        self.button3 = Toggle_button(self, text='Button3')
        self.button3.pack(pady=5)
        self.do_some_label = running_label(self, text='do some')
        self.do_some_label.pack(pady=5)
        self.do_some2_label = running_label(self, text='do some2')
        self.do_some2_label.pack(pady=5)
        self.pack()
        self.do_some_first = False
        self.update_state()

    def update_state(self):
        do_some = self.button1.button_on and self.button2.button_on
        do_some2 = self.button3.button_on

        if do_some and not do_some2:
            self.do_some_first = True
        elif not do_some:
            self.do_some_first = False

        if not self.do_some_first:
            do_some2 = False

        self.do_some_label.running(do_some)
        self.do_some2_label.running(do_some2)

        self.after(500, self.update_state)


if __name__ == '__main__':
    app = tk.Tk()
    main_frame = MainFrame()
    app.mainloop()
Reply


Messages In This Thread
RE: [HELP] Nested conditional? double condition followed by another condition. - by Yoriz - May-31-2020, 08:13 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 486 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  unable to remove all elements from list based on a condition sg_python 3 584 Jan-27-2024, 04:03 PM
Last Post: deanhystad
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 825 Dec-04-2023, 09:48 PM
Last Post: sanky1990
  Sent email based on if condition stewietopg 1 967 Mar-15-2023, 08:54 AM
Last Post: menator01
  Replacing values ​​in Mysql with a condition stsxbel 0 683 Mar-05-2023, 08:20 PM
Last Post: stsxbel
  create new column based on condition arvin 12 2,497 Dec-13-2022, 04:53 PM
Last Post: jefsummers
Question Running an action only if time condition is met alexbca 5 1,429 Oct-27-2022, 02:15 PM
Last Post: alexbca
  How to assign a value to pandas dataframe column rows based on a condition klllmmm 0 912 Sep-08-2022, 06:32 AM
Last Post: klllmmm
  How to write the condition for deleting multiple lines? Lky 3 1,222 Jul-10-2022, 02:28 PM
Last Post: Lky
  Can I check multi condition for 1 item in a easy way? korenron 4 1,660 May-01-2022, 12:43 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020