why tkinter start stop infinite loop doesn't process

Discussion in 'Programmer's Corner' started by pretty_fly, Aug 9, 2017.

  1. pretty_fly

    Thread Starter New Member

    Feb 2, 2015
    20
    0
    I want to start an infinite loop after pressing START button. But I' m having problem for giving the initial values for the internal index. After the loop started it must change the value of j to bigger than 0 and the loop will perform other tasks. However this doesn't happen and the loop doesn't enter "elif" section. So could you give some help how to process this loop ?

    This is the main part of the whole gui code.

    Code (Python):
    1.  
    2. from Tkinter import *
    3. import serial
    4. from threading import Timer
    5.  
    6. port = serial.Serial('/dev/ttyUSB0', 115200)
    7.  
    8. PC1 = open("File_1.txt","a")
    9. PC2 = open("File_2.txt","a")
    10. PC3 = open("File_3.txt","a")
    11. PC4 = open("File_4.txt","a")
    12. PC5 = open("File_5.txt","a")
    13. PC6 = open("File_6.txt","a")
    14. PC7 = open("File_7.txt","a")
    15. PC8 = open("File_8.txt","a")
    16.  
    17.  
    18. HMCC=Tk()
    19.  
    20. HMCC.title(" GUIs v1.0 ")
    21. HMCC.geometry("750x500")
    22.  
    23. control_var = 0
    24.  
    25. def start():
    26.     global t
    27.     t=Timer(1,start)
    28.     t.start()
    29.     print("Data Transmission started ... ! ")
    30.     global control_var
    31.     control_var= 1
    32.  
    33.  
    34. def stop():
    35.     global control_var
    36.     control_var = 0
    37.     global t
    38.     if t:
    39.         t.cancel()  
    40.  
    41.  
    42. def scanning():
    43.     j=0
    44.     k=0
    45.     m=[]
    46.     ii=0
    47.     global control_var
    48.  
    49.     if control_var == 1 :
    50.  
    51.         print "Running..."
    52.      
    53.         raw_data     = ord(port.read())
    54.         bin_raw_data = bin(raw_data)[2:].zfill(8)
    55.         print(raw_data)
    56.      
    57.         if raw_data==127:  
    58.             k=0
    59.             j=j+1
    60.             print '\n-----',j,'------------------------------ \n'
    61.             m=[]
    62.      
    63.         elif j > 0 :
    64.          
    65.             m.append(bin_raw_data)
    66.             k=k+1
    67.             if k == 24    :  
    68.                 for ii in range(0,8) :
    69.                  
    70.                     Count_bin=m[ii*3]+m[ii*3+1]+m[ii*3+2]
    71.                     Count=int(Count_bin,2)
    72.                  
    73.                     print 'Channel:',ii+1,'  ' , Count
    74.                  
    75.                     if   ii==0:  
    76.                         PC1.write(str(Count) + "\n")
    77.                         count_1.config(raw_data)                      
    78.                     elif ii==1:  
    79.                         PC2.write(str(Count) + "\n")
    80.                         count_2.config(raw_data)                      
    81.                     elif ii==2:  
    82.                         PC3.write(str(Count) + "\n")
    83.                         count_3.config(raw_data)                      
    84.                     elif ii==3:  
    85.                         PC4.write(str(Count) + "\n")
    86.                         count_4.config(raw_data)                      
    87.                     elif ii==4:  
    88.                         PC5.write(str(Count) + "\n")
    89.                         count_5.config(raw_data)                      
    90.                     elif ii==5:  
    91.                         PC6.write(str(Count) + "\n")
    92.                         count_6.config(raw_data)                      
    93.                     elif ii==6:  
    94.                         PC7.write(str(Count) + "\n")
    95.                         count_7.config(raw_data)                      
    96.                     elif ii==7:
    97.                         PC8.write(str(Count) + "\n")
    98.                         count_8.config(raw_data)                      
    99.  
    100.  
    101. count_1 = Label(HMCC)
    102. count_2 = Label(HMCC)
    103. count_3 = Label(HMCC)
    104. count_4 = Label(HMCC)
    105. count_5 = Label(HMCC)
    106. count_6 = Label(HMCC)
    107. count_7 = Label(HMCC)
    108. count_8 = Label(HMCC)
    109.  
    110. count_1.place(relx=.6, rely=.1, anchor="c")
    111. count_2.place(relx=.6, rely=.2, anchor="c")
    112. count_3.place(relx=.6, rely=.3, anchor="c")
    113. count_4.place(relx=.6, rely=.4, anchor="c")
    114. count_5.place(relx=.6, rely=.5, anchor="c")
    115. count_6.place(relx=.6, rely=.6, anchor="c")
    116. count_7.place(relx=.6, rely=.7, anchor="c")
    117. count_8.place(relx=.6, rely=.8, anchor="c")
    118.  
    119.  
    120. Channel_1 = Label(HMCC, text = "Channel 1 : ")
    121. Channel_2 = Label(HMCC, text = "Channel 2 : ")
    122. Channel_3 = Label(HMCC, text = "Channel 3 : ")
    123. Channel_4 = Label(HMCC, text = "Channel 4 : ")
    124. Channel_5 = Label(HMCC, text = "Channel 5 : ")
    125. Channel_6 = Label(HMCC, text = "Channel 6 : ")
    126. Channel_7 = Label(HMCC, text = "Channel 7 : ")
    127. Channel_8 = Label(HMCC, text = "Channel 8 : ")
    128.  
    129. Channel_1.place(relx=.4, rely=.1, anchor="c")
    130. Channel_2.place(relx=.4, rely=.2, anchor="c")
    131. Channel_3.place(relx=.4, rely=.3, anchor="c")
    132. Channel_4.place(relx=.4, rely=.4, anchor="c")
    133. Channel_5.place(relx=.4, rely=.5, anchor="c")
    134. Channel_6.place(relx=.4, rely=.6, anchor="c")
    135. Channel_7.place(relx=.4, rely=.7, anchor="c")
    136. Channel_8.place(relx=.4, rely=.8, anchor="c")
    137.  
    138. button1 = Button(text="   START   " , fg="black"  ,command=start )
    139. button2 = Button(text=" PAUSE "     , fg="black" )
    140. button3 = Button(text="    STOP    ", fg="black",command=stop)
    141. button4 = Button(text="QUIT"        , fg="black",command=HMCC.quit)
    142.  
    143. button1.place(relx=.1, rely=.2, anchor="c")
    144. button2.place(relx=.1, rely=.3, anchor="c")
    145. button3.place(relx=.1, rely=.4, anchor="c")
    146. button4.place(relx=.1, rely=.5, anchor="c")
    147.  
    148.  
    149.  
    150. HMCC.mainloop()
    151.  
    152. PC1.close()
    153. PC2.close()
    154. PC3.close()
    155. PC4.close()
    156. PC5.close()
    157. PC6.close()
    158. PC7.close()
    159. PC8.close()
    160.  
    161.  
    Thanks in advance ...
     
    Last edited: Aug 13, 2017
  2. Motanache

    Member

    Mar 2, 2015
    316
    25
    If you want, please, attach this piece of code as a small program that can be compiled.

    Also indicates the compiler.
    I would like to compile it too.
     
  3. pretty_fly

    Thread Starter New Member

    Feb 2, 2015
    20
    0
    Hi @Motanache

    Thank you for your interest.
    I've uploaded the complete GUI script. I just type "sudo python name_of_the_script.py" to open the GUI.
     
  4. Motanache

    Member

    Mar 2, 2015
    316
    25
    Unfortunately, I was unable to run it.You also use a USB-serial converter.
    py.png
    I do not think I can answer.
     
    Last edited: Aug 13, 2017
Loading...