当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Waiting inside a for loop for button click in Tkinter

问题描述:

  1. I am trying create a for loop based on the decimal value of the option menu button.
  2. If the option button chosen is 2, then the for loop circulates for two times.
  3. But the for loop needs to wait until the button created inside the for loop gets clicked.
  4. Below code shows the same.

    • Option menu has range of 1 to 10 i.e. user can choose 1 to 10 device to be created.
    • The moment the option menu is choose, the command button for the option menu gets operated i.e. comp_gt_data.
    • This basically circulates in a for loop to take the configuration of the N number of devices.
    • Once after taking the configuration for the 1st device, I create a "Enter the Next Device Configuration" button inside the for loop and wanted the button to be clicked to take the next device configuraiton.
    • But the "Enter Next Device ...." button is not getting created and keeps circulating in while loop.
    • I tried using time.sleep(60) but still didn't work.
    • The "Enter Next Device.." basically tries to clear the old device configuration and tries to set cvadt_val_loop_exit=0 so that the while loop can exit.
    • QUERY: HOW TO Basically wait inside a for loop for a button to be clicked to proceed further?


self.cvo1 = OptionMenu(root, cvagt_val, *cvagt_list, command=self.comp_gt_data)

self.cvo1.pack()

def comp_gt_data(self, *args):

if cvagt_val.get()!=0:

cvagt_val_loop = cvagt_val.get()

for cvagt_loop in range(1, cvagt_val_loop+1):

self.cvad_l0 = Label(root, text="Enter the %d Device Config"% cvagt_loop)

self.cvad_l0.pack()

self.cvad_l1 = Label(root, text="How Many D~S You Want To Create: ")

self.cvad_l1.pack()

self.cvad_e1 = Entry(root)

self.cvad_e1.pack()

self.cvad_l2 = Label(root, text="How Many MS You Want To Create: ")

self.cvad_l2.pack()

self.cvad_e2 = Entry(root)

self.cvad_e2.pack()

self.cvad_l3 = Label(root, text="How Many SD You Want To Create: ")

self.cvad_l3.pack()

self.cvad_e3 = Entry(root)

self.cvad_e3.pack()

if cvagt_loop < cvagt_val_loop:

if not (self.cv_next_dev_cfg_button):

self.cv_next_dev_cfg_button = Button(root, text="Enter Next Device Configuration", command=self.cv_next_dev_cfg_button_code)

self.cv_next_dev_cfg_button.pack()

# Waiting For cvadt_val_loop_exit bit to get set when the cv_next_dev_cfg_button is clicked but its not happening and circulating in the while loop

cvadt_val_loop_exit = 1

while(cvadt_val_loop_exit):

print "Waiting For Next Device Config Button To Be Hit"

time.sleep(60)

else:

self.cv_next_dev_cfg_button_code()

if not (self.return_button):

self.return_button = Button(root, text="Return To Main Menu", command=self.return_button_code)

self.return_button.pack()

else:

print "Choose Atleast One Device To Create"

def cv_next_dev_cfg_button_code(self):

if (self.cvad_l0):

self.cvad_l0.pack_forget(); self.cvad_l0.destroy(); self.cvad_l0 = None

if (self.cvad_l1):

self.cvad_l1.pack_forget(); self.cvad_l1.destroy(); self.cvad_l1 = None

if (self.cvad_l2):

self.cvad_l2.pack_forget(); self.cvad_l2.destroy(); self.cvad_l2 = None

if (self.cvad_l3):

self.cvad_l3.pack_forget(); self.cvad_l3.destroy(); self.cvad_l3 = None

if (self.cvad_e1):

self.cvad_e1.pack_forget(); self.cvad_e1.destroy(); self.cvad_e1 = None

if (self.cvad_e2):

self.cvad_e2.pack_forget(); self.cvad_e2.destroy(); self.cvad_e2 = None

if (self.cvad_e3):

self.cvad_e3.pack_forget(); self.cvad_e3.destroy(); self.cvad_e3 = None

if (self.cv_next_agt_cfg_button):

self.cv_next_agt_cfg_button.pack_forget(); self.cv_next_agt_cfg_button.destroy(); self.cv_next_agt_cfg_button = None

cvadt_val_loop_exit = 0

网友答案:

A for() is not the answer here. Instead call a function the correct number of times and wait however long you want in between. This should give you enough to get started.

try:
    import Tkinter as tk     ## Python 2.x
except ImportError:
    import tkinter as tk     ## Python 3.x

class ButtonForLoop():
    def __init__(self, top):
        self.top=top
        self.but_1=tk.Button(top, text="Start for()", command=self.start_for)
        self.but_1.grid(row=0, column=0)

        self.num_for=0
        ## for simplicity, assume 3 iterations through the loop
        self.max_num=3

    def next_pass(self):
        ## create button again
        self.but_1=tk.Button(top, text="Pass # %d" % (self.num_for),
                             command=self.start_for)
        self.but_1.grid(row=0, column=0)

    def start_for(self):
        """ button clicked so call next_pass() the correct
            number of iterations
        """
        ## destroy whatever button is showing
        self.but_1.destroy()
        if self.num_for < self.max_num:
            self.num_for += 1
            self.top.after(500, self.next_pass)
        else:
            self.top.quit()

top=tk.Tk()
BF=ButtonForLoop(top)
top. mainloop()
分享给朋友:
您可能感兴趣的文章:
随机阅读: